|
Hi and thanks for looking,
Just started to learn Java and after reading various tutorials came up with this simple tables program.
My question, is it as it should be? I mean it works OK but is it coded correctly?
import java.util.Scanner;
public class MyTables {
public static void main(String[] args) {
int secondNum, answerNum;
Scanner sc = new Scanner(System.in);
System.out.println("Type a number:");
secondNum = sc.nextInt();
for (int firstNum = 1; firstNum <= 12; firstNum ++) {
answerNum = firstNum * secondNum;
System.out.println(firstNum + " x " + secondNum + " = " + answerNum);
}
}
}
|
|
|
|
|
It looks reasonable to me. The only thing I would say is that it is generally better to start loop counters from zero, as they are often used to access array items:
for (int firstNum = 0; firstNum < 12; firstNum ++) {
answerNum = firstNum * secondNum;
System.out.println(firstNum + " x " + secondNum + " = " + answerNum);
}
Also try to use consistent indentation, as it makes your code much easier to read. I don't know which tutorials you have been working on but The Java™ Tutorials[^] are some of the best for a beginner.
|
|
|
|
|
I have coded for Sudoku puzzle in java.The thing is my code has limitation for giving inputs for 9*9 grid. How do I make my code adaptable for all the grids.Please have patience.I am new to java.
What changes do I need to make so that the code can run on all grid sizes?The grid is square not a rectangle.
class Solution {
public void solveSudoku(char[][] board) {
if(solveSudoku2(board)) {
return;
}
}
public boolean solveSudoku2(char[][] board) {
boolean isEmpty = true;
int row = -1;
int col = -1;
int n = board.length;
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(board[i][j] == '.') {
row = i;
col = j;
isEmpty = false;
break;
}
}
if(!isEmpty) {
break;
}
}
if(isEmpty) {
return true;
}
for(int num = 1; num <= n; num++) {
char char_num = (char)(num + '0');
if(checkSafe(board,char_num,row,col)) {
board[row][col] = (char)(num + '0');
if(solveSudoku2(board)) {
return true;
} else {
board[row][col] = '.';
}
}
}
return false;
}
public boolean checkSafe(char[][] board, char num, int row, int col) {
for(int i = 0; i< board.length; i++ ) {
if(board[row][i] == num) {
return false;
}
}
for(int j = 0; j < board[0].length; j++) {
if(board[j][col] == num) {
return false;
}
}
int checknum = (int)Math.sqrt(board.length);
int startrow = row - row % checknum;
int startcol = col - col % checknum;
for(int k = startrow; k < startrow + checknum; k++) {
for(int l = startcol; l < startcol + checknum; l++) {
if(board[k][l] == num) {
return false;
}
}
}
return true;
}
}
|
|
|
|
|
Sudoku size is is limited by the values that are allowed to be placed in the squares. For digits it is 9 x 9, for alphabetics it could be any number you choose. The main problem is the complexity of the solution as the size increases.
|
|
|
|
|
package Array;
import java.util.Arrays;
public class Homework {
public static int [] sortirovka (int [] array) {
int a;
for (int i = 0; i < array.length; i++) {
int min = array [i];
int index = i;
for (int j = i + 1; j < array.length; j++) {
if (array [j] < min) {
min = array [j];
index = j;
}
}
if (i != index) {
a = array[i];
array[i]= min;
array [index] = a;
}
}
return array;
}
public static void main(String[] args) {
int [] array = {5, 2, 5, 3, 8, 9, 10, -2, 0, 6};
sortirovka (array);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
|
|
|
|
|
Yes, quite normal, programming takes some time to learn. You can learn the basics (at your own speed) from The Java™ Tutorials[^].
|
|
|
|
|
Thank you very much, appreciate it.
|
|
|
|
|
You're welcome. Bear in mind that every 'expert' had to start learning from zero at one time.
|
|
|
|
|
Write A Program In Java That Displays A Cuckoo Clock that plays computer time
|
|
|
|
|
Nobody here will do your homework assignment for you, no matter how many times you repost it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
please who can help me whit
java applet cuckoo clock that plays computer time
|
|
|
|
|
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I've done java as a hobby for a few years, mostly tutorials. I'm working on a few Android tutorials currently, along with Spring framework, and occasionally Python stuff.
I've built several Java projects with tutorials, like a JavaFx email client. I want to build a project on my own. Something not too complicated that will look good to employers.
Does anyone have any suggestions?
|
|
|
|
|
Employers are rarely impressed with work done as a hobby. They are generally looking for experience in the fields that they use in their business. So you should first look around at what jobs are being advertised in the trade press, talk to recruitment agents to see what is needed, etc.
|
|
|
|
|
There are some "employers" who think that if you're really into "programming", then you'll always have some side project going (because you have no life).
And / or, they want you to be a (well known) "blogger"; so they can paint their "About" page with your face (and claim you as one of their own).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Fortunately I have never worked for any such 'enlightened' companies.
|
|
|
|
|
I'm using Wildfly 20.0 version.
I would like to use JDBC in Wldfly for my dynamic web project (with JSF and EJB). So I'm looking for configuring the server to handle security by JDBC connection to database which stores user/role/userrole.
What can I to for my purpose?
|
|
|
|
|
|
I'm reading this guide, very interesting. I will ask you about it when I have more specific doubts.
|
|
|
|
|
I have configured Realm with JDBC, but when I try to login I get this message in Console
TRACE [org.jboss.as.domain.management.security] (default task-1) User 'UserNameDGU' not found in properties file.
and before I have seen this:
DEBUG [org.wildfly.security] (management I/O-1) Unable to create instance: java.security.NoSuchAlgorithmException: Service not registered with Provider WildFlyElytron: WildFlyElytron: SaslServerFactory.DIGEST-SHA -> org.wildfly.security.sasl.digest.DigestServerFactory
|
|
|
|
|
write a general python program that takes into account:
1. The cost of your ideal house. (basic)
2. The cost needed for a down payment. (basic)
3. Your annual salary. (basic)
4. The portion of your salary that you will
|
|
|
|
|
Apart from the fact that point 4 has been cut off early, there is no information about what the program is supposed to do. I could write the following:
def somethings():
print("Ideal house cost £295,000")
print("Down payment £29,500")
print("Annual salary £57,000")
print("The portion of my salary that I will")
But as you can see it is meaningless.
|
|
|
|
|
Message Closed
modified 29-Oct-20 4:20am.
|
|
|
|
|
First off this is nothing to do with Java, so it really belongs in Quick Answers[^] .
Secondly, do not expect people here to do your work for you. Show the code that you have written, explain what does not work (in proper details) and people will try to help you. But remember, this is your assignment, so you are expected to solve it.
|
|
|
|