|
|
Thanks Richard
I seem to remember it being easier in vb but could be looking through rose coloured specs.
I looked at the links you suggested but to no avail mind they are 12yrs old now so maybe not surprising.
I will try to use another idea instead.
I was trying to get a question with three possible answers and the user had to drag the correct answer in place, so maybe I'll look at seeing if a jTextField can be dragged around.
If that fails three radio buttons and they can click the one they think is the right answer.
I will stick with Java
|
|
|
|
|
I have created a method
void getSecond(int secondNum)
and in each of my radio buttons I have the following two pieces of code to return a number to the method
both work fine but just wondered which is better practice?
First
getSecond(11);
Second
getSecond(Integer.parseInt(times12.getName()));
|
|
|
|
|
There is no "better"; and if you can't explain how the statements differ, then the answer is "11", or "42".
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Thanks for the reply Eddy,
Glad to hear there is no 'Better' that's good to know.
Of course I can explain how they differ, both return a number to the method one returns the name of the radio button it refers to the other returns the number placed in the buttons event.
So I will use the one that returns the name of the button as it can eliminate the mistake of returning a number for a different button.
But as there is no 'Better' way, then I guess it's how ever the programmer feels on the day.
|
|
|
|
|
Hi
I am looking at Netbeans and have in a button control the following code;
int firstNum, answerNum = 0;
for (firstNum = 1; firstNum <= 12; firstNum ++)
answerNum = firstNum * 2;
displayTable.setText(firstNum + " x 2 = " + answerNum);
but in the textarea it only shows 13 x 2 = 24
does the for not work in netbeans? or does it have to be done differently.
|
|
|
|
|
That's exactly what I'd expect.
When firstNum is 12, answerNum is set to 24.
Then the for is executed again, increments firstNum to 13, then the test fails, so it goes on to display what you see.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Thanks Peter,
So my question is how do I get it to display in the Textarea as it does when i run it in the cmd
Like this,
1 x 2 = 2
2 x 2 = 4
3 x 2 = 6
etc?
|
|
|
|
|
This is not netbeans (which is an IDE), it is Java code. You need to understand the difference. As to your question: You only have a single line being processed in the for loop. You need to put both lines inside a block thus:
for (firstNum = 1; firstNum <= 12; firstNum ++) {
answerNum = firstNum * 2;
displayTable.setText(firstNum + " x 2 = " + answerNum);
}
It is good practice to use braces in all repeat blocks, even those with only a single line. It means if you ever add another line to the loop it will always be inside the block.
|
|
|
|
|
Thanks for the reply and advice it now works as I wanted it to.
I changed the code as follows using "\n" to give me a new line each time it loops;
private void twoButtonActionPerformed(java.awt.event.ActionEvent evt) {
int firstNum, answerNum = 0;
for (firstNum = 1; firstNum <=12; firstNum ++){
answerNum = firstNum * 2;
displayTable.append(firstNum + " x 2 = " + answerNum +"\n");
}
}
In summing up then, Netbeans is developed by Apache and is just an environment to make it easier to develop Java with a windows style, similar to Visual Basic but using Java in the buttons action event. Yes?
|
|
|
|
|
MallardsReach wrote: is just an environment to make it easier to develop Yes, it is an Integrated Development Environment, similar to Visual Studio, eclipse etc. It is designed to provide editors and debuggers for different languages, and automatic compilation. The actual final product does not need to beWindows style, although it helps if the IDE also has a visual design feature so you can see the layout of your form as you create it. In the old days you had to write all the Windows code by hand, build and run, before you could see what it looked like.
|
|
|
|
|
I did write code many years ago, Spectum, Amos and VB but so much has changed although still similar, I'm liking Java just need to get used to the syntax.
The IDE does have a design feature so you can set up how it will look when run and then start placing the code in the buttons, checkboxes etc it even has preview design as well.
No doubt I will be back at the forum again but in the mean time Thanks for all the help and guidance.
|
|
|
|
|
You're welcome. Maybe you will be answering questions in the future.
|
|
|
|
|
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);
}
}
}
|
|
|
|
|
Looks OK and works, so that should be enough. You may like to use formatting on your output so all table entries are on the same column - currently the x and beyond is shifted right from 10 onwards.
|
|
|
|
|
Thanks for the reply and for the suggestion re formatting.
|
|
|
|
|
books examples of precedures stored for sqlserver 
|
|
|
|
|
|
Search on Amazon. There are tons of them.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Question: Write a Java program for a 2-dice game. This game will roll the two dice together for 100 times. In each time, it will display the face values from both dice. The program will count how many times (out of these 100 attempts) did the two dice generate a total point of 10 or above, and display the result at the end. (10 pts) See the execution example above.
Requirements:
a) In this question, use the Random class and its object to simulate dice rolling. Make sure your dice will generate integer values in the range of 1 - 6.
b) You will need two counter variables. One is to count for how many attempts you have rolled the two dice – to control the loop. The other is to count how many times that your two dices gave a total point of 10 or above.
c) Use a WHILE loops in this program. DO NOT USE THE OTHER LOOP METHODS in this question.
d) Your class should be named TwoDiceGame, and your source code file should be named TwoDiceGame.java.
e) Programming styles are always required.
My code:
import java.util.Random;
public class TwoDiceGame {
public static void main(String[] args){
// Declare a counter variable to count the loop time
int count = 0;
int die1, die2;
for(int i=0;i<100;i++)
rNums = new Random(); // create a new object of Random class
int die1 = rNums.nextInt(6)+1;
int die2= rNums.nextInt(6)+1;
if(Die1 + Die2 >= 10);
count ++;
}
System.out.println ("Out of 100 rolls of the two dice, the total of both the dice greater than or equal to 10 was %d times");
}
{
}
Errors:
----jGRASP exec: javac -g TwoDiceGame.java
TwoDiceGame.java:32: error: <identifier> expected
System.out.println ("Out of 100 rolls of the two dice, the total of both the dice greater than or equal to 10 was %d times");
^
TwoDiceGame.java:32: error: illegal start of type
System.out.println ("Out of 100 rolls of the two dice, the total of both the dice greater than or equal to 10 was %d times");
^
TwoDiceGame.java:34: error: class, interface, or enum expected
{
^
3 errors
----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
Can someone please help me revise this code? I have this due by tonight and have tried everything to fix it.
|
|
|
|
|
The call to System.out.println is outside of the main method, which is incorrect. Using proper indentation helps to see such basic errors.
It should be
import java.util.Random;
public class TwoDiceGame {
public static void main(String[] args){
int count = 0;
int die1, die2;
for(int i=0;i<100;i++) {
rNums = new Random();
int die1 = rNums.nextInt(6)+1;
int die2= rNums.nextInt(6)+1;
if(Die1 + Die2 >= 10);
count ++;
}
System.out.println ("Out of 100 rolls of the two dice, the total of both the dice greater than or equal to 10 was %d times");
}
}
|
|
|
|
|
I also just noticed that the instructions tell you to use a "while" loop and not any other type.
|
|
|
|
|
Try to use another loop...
|
|
|
|
|
|
Here's what I came up with not sating it's the correct way as I am only just starting with Java but it works.
import java.util.Random;
public class TwoDiceGame {
public static void main(String[] args){
int count = 0, total = 0;
for(int i=0;i<100;i++){
Random rNums = new Random();
int rNums_die1 = rNums.nextInt(6)+1;
int rNums_die2 = rNums.nextInt(6)+1;
if(rNums_die1 + rNums_die2 >= 10){
total ++;
count ++;
}
else{
count++;
}
}
System.out.println ("Out of 100 rolls of the two dice, the total of both the dice greater than or equal to 10 was " + total + " times");
}
}
|
|
|
|