This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have a problem with my code, it's a simple dice game:
import java.util.Random;
class Zaruri {
public static void main(String args[]) {
Random roll = new Random();
int[] zar = new int[2];
for (int i = 0; i < 1; i++) {
for(int k = 0; k < 2; k++){
zar[i] = (int) (roll* 6) + 1;
}
if (zar[0] == zar[1]) {
System.out.println("Your numbers are : " + zar[0] + " and " + zar[1] + "\nYou won! \nYEEEY!!");
} else {
System.out.println("Your numbers are : " + zar[0] + " and " + zar[1] + "\nYou lost, better luck next time!");
}
}
}
}
I don't know how to make it work,first it won't let print out the second dice(it comes allways 0) and if i put more then 1 roll and (roll.nextDouble * 6) + 1; it will work,but i don't want more then 1 roll.
CAn you help me ? What am i doing wrong??
You don't need 2 for loops. Just 1 loop will do. Remove the outer i loop and let k loop do your random int generation.
for (int k = 0; k < 2; k++) {
zar[k] = roll.nextInt(6) + 1; // k loop populates your array.
}
if (zar[0] == zar[1]) {
System.out.println("Your numbers are : " + zar[0] + " and "
+ zar[1] + "\nYou won! \nYEEEY!!");
} else {
System.out.println("Your numbers are : " + zar[0] + " and "
+ zar[1] + "\nYou lost, better luck next time!");
}
There are a number of issues, i'll go through them 1 by 1
for (int i = 0; i < 1; i++) {
for(int k = 0; k < 2; k++){
zar[i] = (int) (roll* 6) + 1;
}
<snip rest of loop>
The loop with k in does nothing but repeat the line zar[i] = (int) (roll* 6) + 1; several times, it does nothing, this loop should be removed
for (int i = 0; i < 1; i++) {
for(int k = 0; k < 2; k++){
zar[i] = (int) (roll* 6) + 1;
}
if (zar[0] == zar[1]) {
System.out.println("Your numbers are : " + zar[0] + " and " + zar[1] + "\nYou won! \nYEEEY!!");
} else {
System.out.println("Your numbers are : " + zar[0] + " and " + zar[1] + "\nYou lost, better luck next time!");
}
}
The print statements are within the for loop, so it prints out every time through the loop, the first time through the loop only the first die will be set, the second time only the first two, etc etc.
zar[i] = (int) (roll* 6) + 1;
roll is of class random, it is not a number but generates random numbers, this should be
zar[i] = (int) (roll.nextDouble()* 6) + 1;
or more sensibly (as you want an int in the end anyway
zar[i] = roll.nextInt()+ 1;
for (int i = 0; i < 1; i++)
Numbers from i=0 to i<1 is just one number, 0, dice 1 is never set because its only goes through the loop once
Try to use Math.random() instead of Random()
I don't see why you have two loops, and especially the one with k as an index. You are not using that k anywhere. Try this:
for (int i = 0; i < 2; i++) {
zar[i] = roll.nextInt(6) + 1;
}
try this instead :
int Low = 10;
int High = 100;
int R = r.nextInt(High-Low) + Low;
SOURCE : How can I generate random number in specific range in Android?
or Java Generate Random Number Between Two Given Values
Delete the first loop with the i and let only the k loop.
Then it won't logically work and always print 0 because you don't generate a random number.
You must do zar[i]=roll.nextInt(6)+1; because the range is from 0 (included) and 6 (excluded). Logically you must add 1 whatever will be the result to obtain a 1-6 range.
Related
public class dice1 {
public static final int N = 6000000;
public static void main(String[] args) {
int[] d = new int[7];
for (int i = 1; i < 7; i++) d[i] = 0;
for (int k = 0; k < N; k++) {
int roll = (int)(6 * Math.random() + 1);
d[roll]++;
}
System.out.println("Rolls: " + N);
for (int i = 1; i < 7; i++) {
float decimal = d[i] / N;
System.out.print(" " + i + ": " + d[i]);
System.out.printf( ", " + i + " was rolled %f" , decimal);
System.out.println();
}
}
}
}
My problem is I have to display it in decimal form. For example on the side with 6 dots it could be 1 mil times out of 6 mil so the output should be like ".16". Mine ends up 0.00. Any tips?
Since both d[i] and N are ints, you're performing integer division, which only retains the "whole" part, left of the decimal point. Since d[i] is smaller than N, you're getting 0.
You could solve this by casting one of them to a floating point type, e.g.:
float decimal = ((float) d[i]) / N;
Or just define them like that to beging with.
I just started programming for few days.
I want to ask the user different questions and if they type in the right question then I will give them a score of 5 marks for getting the question right, plus bonus marks which are given by using Random, this bonus(Random) is then added to the total score(score(5) + random) which is stored and used to add the next score of the following question and the process repeats with all the questions[5].
This is what I have done so far, but it keeps printing the same result for every question and I want to keep adding to the previous score.
for (int attempts = 1; attempts <= 3; attempts++);
{
Random dice = new Random();
for(int n = 0; n <QArray.length; n++)
{
System.out.println("Question" + (n+1));
System.out.println(QArray[n]);
for(int m =0; m<3; m++)
{
String ans = scanner.nextLine();
int t = dice.nextInt(9) + 1;
int scoremarks = 5;
if (ans.equalsIgnoreCase(AArray[n]))
{
System.out.println("That is correct!\nYour score is:" + scoremarks + "\nWith virtual dice your total score is:" + (scoremarks +t));
break;
}
else
{
System.out.println("That is incorrect!\nYou got 0 Marks\nYour score is 0!");
}
You need to maintain the score outside the loop. You can print totalScore after all the questions and will have the total score of all the right answers.
int totalScore = 0;
for (int n = 0; n < QArray.length; n++) {
System.out.println("Question" + (n + 1));
System.out.println(QArray[n]);
for (int m = 0; m < 3; m++) {
String ans = scanner.nextLine();
Random dice = new Random();
int t = dice.nextInt(9) + 1;
int scoremarks = 5;
if (ans.equalsIgnoreCase(AArray[n])) {
totalScore += (scoremarks + t);
System.out.println("That is correct!\nYour score is:" + scoremarks + "\nWith bonus your total score is:" + (scoremarks + t));
// correct = false;
break;
} else {
System.out.println("That is incorrect!\nYou got 0 Marks\nYour score is 0!");
}
}
}
With a few optimizations as mentioned in the comments:
int totalScore = 0;
Random dice = new Random();
int scoremarks = 5;
for (int n = 0; n < QArray.length; n++) {
System.out.println("Question" + (n + 1));
System.out.println(QArray[n]);
for (int m = 0; m < 3; m++) {
String ans = scanner.nextLine();
int t = dice.nextInt(9) + 1;
if (ans.equalsIgnoreCase(AArray[n])) {
totalScore += (scoremarks + t);
System.out.println("That is correct!\nYour score is:" + scoremarks + "\nWith bonus your total score is:" + (scoremarks + t));
break;
} else {
System.out.println("That is incorrect!\nYou got 0 Marks\nYour score is 0!");
}
}
}
You can print totalScore in the condition when the answer is correct to see score increase each time the answer is correct. Also for the case when the answer is not correct, you can still show the total score to see how many points you might have from the previous questions. Not sure if there are 3 tries for getting the answer right but that's what the inside for loop seems to be doing so the message for incorrect guess seems inappropriate.
This is my code so far. It prints the numbers 0 through 10. But I can’t figure out how to multiply each number by 2 and 10.
while(numberCounter <= 9){
System.out.println("Number: " + numberCounter);
numberCounter++;
}
Well, what you are doing is probably better suited for a for loop. Like this:
for (int i = 0; i < 10; i++) {
System.out.println("Number: " + i); // This prints 0-9
}
Or for each number times 2:
for (int i = 0; i < 10; i++) {
System.out.println("Number: " + (i*2)); // This prints 0-9 times 2
}
Or for each number times 10:
for (int i = 0; i < 10; i++) {
System.out.println("Number: " + (i*10)); // Prints 0-9 times 10
}
Lastly, each number times 10 times 2:
for (int i = 0; i < 10; i++) {
System.out.println("Number: " + (i*10*2)); // Prints 0-9 times 10 times 2
}
You should take a class on Java or read a thorough tutorial or book, because these are very basic Java topics. Try this one: http://www.learnjavaonline.org/
How can I use logic loop to print out 20 Pythagorean numbers of non-congruent triangles.
Without repeating numbers i.e if I have 4,3,5 I can't have 3,4,5.
I was using "for" loops but I don't know how to remove the repeating answers.
for (k = 0; k < 50; k++)
{
for ( i = 0; i < 50; i++)
{
for ( j = 0; j < 50; j++)
{
if ( (k+1)*(k+1) + (i+1)*(i+1) == (j+1)*(j+1) )
{
System.out.println( "\n\n\t\tThe numbers are : " + (k+1) + ", "
+ (i+1) + ", "
+ (j+1) );
}
}
}
}
Two (related) options come to mind: You could "keep" each of your triples in a sorted order, so when you find {4,3,5} you turn it into {3,4,5} before saving it for subsequent comparison with any others for uniqueness. Or, you could create a "Triple" class where you define a method like boolean equals(final Triple rval) that does the comparison of each element from lowest to highest. Of course, there are probably other ways this could be done as well.
UPDATE: Given the code you just added, if you only want to print them out, then you probably don't need to keep the triples you've found so far around as I was assuming above. The following modification to your code might work:
for (k = 0; k < 50; k++)
{
for ( i = k; i < 50; i++)
{
for ( j = i; j < 50; j++)
{
if ( (k+1)*(k+1) + (i+1)*(i+1) == (j+1)*(j+1) )
{
System.out.println( "\n\n\t\tThe numbers are : " + (k+1) + ", "
+ (i+1) + ", "
+ (j+1) );
}
}
}
}
Note that I changed the starting point of the inner loops to ensure the following holds of all of the triples you will find: k <= i <= j. I believe that this constraint will also ensure uniqueness.
i have to Have the computer compute all the possible ways three dice can be thrown: 1 + 1 + 1, 1 + 1 + 2, 1 + 1 + 3, etc. Add up each of these possibilities and see how many give nine as the result and how many give ten.
public class prog209b
{
public static void main(String []args){
int sum = 0;
int count = 0;
do{
for(int i = 1; i<=6; i++){
count +=1;
for(int y=1; y<=6; y++){
count += 1;
for(int x=1; x<=6; x++ ){
sum = i + y + x;
}
}
}
}while (sum == 10 && count == 27);{
System.out.println("There are " +count +" ways to get ten");
}
}
}
Thats what i came up with but i can get it to work correctly at all. instead of giving me that theres 27 ways it gives me like 42. Obviously im not doing this correctly. Please help me before i have an aneurysm
I'm not going to do your homework for you but:
You don't need the do/while loop - why would you need to keep going once you'd found all the possibilities?
You don't need to count all the possible dice rolls - you need to count how many give 9 as a total, and how many give 10. You could either do that with two variables, or you could make a method which took the "target" as a parameter
You don't need the sum other than right in the innermost loop - all you need to do is find out whether the sum of the values is equal to one of your target values, and increment the appropriate counter...
Your count += 1s are in the wrong place and your while (sum == 10 && count == 27) makes no sense.
int nine = 0
int ten = 0;
for(int i = 1; i<=6; i++){
for(int y=1; y<=6; y++){
for(int x=1; x<=6; x++ ){
sum = i + y + x;
if (sum == 9) nine++;
if (sum == 10) ten++;
}
}
}
You should do something like this:
for(int i = 1; i<=6; i++){
for(int y=1; y<=6; y++){
for(int x=1; x<=6; x++ ){
sum = i + y + x;
if (sum == 10)
count++;
}
}
}
System.out.println("There are " + count + " ways to make 10");