I have code which should work, but the braces at the bottom give me an error. There is probably a simple solution that I can't find, probably because I am exhausted. Both of these codes are in different classes. If I erase a brace then another brace gives me an error.
import java.util.Scanner;
public class question {
public static void main(String[] args) {
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int f = 0;
int grade = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter grade scores: ");
do {
System.out.print("Grade: ");
grade = keyboard.nextInt();
if (grade >= 0 && grade < 60) {
f++;
} else if (grade < 70) {
d++;
} else if (grade < 80) {
c++;
} else if (grade < 90) {
b++;
} else if (grade <= 100) {
a++;
}
while (grade >= 0) {
System.out.println("Total scores: " + (a + b + c + d + f));
System.out.println("A: " + a);
System.out.println("B: " + b);
System.out.println("C: " + c);
System.out.println("D: " + d);
System.out.println("F: " + f);
}
} // this one gives error
}
}
The syntax is
do {
} while ( boolean expression) ;
You're missing the while part
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
If you read the documentation this is what a do while loop should look like:
do {
statement(s)
} while (expression);
What you currently have is:
do {
}
and so you are missing the while (expression) ; portion.
You're missing the 'while' part in your code. A do-while loop has to have a condition to check against after each iteration. Your syntax should look something like:
do
{
//code here
}
while (condition-here);
Note: do-while loops take a semi-colon at the end of the statement. Don't miss that
I think you are trying to do this:
public static void main(String[] args){
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int f = 0;
int grade = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter grade scores: ");
do{
System.out.print("Grade: ");
grade = keyboard.nextInt();
if(grade >= 0 && grade < 60){
f++;
}
else if(grade < 70){
d++;
}
else if(grade < 80){
c++;
}
else if(grade < 90){
b++;
}
else if(grade <= 100){
a++;
}
} while (grade >= 0)
System.out.println("Total scores: " + (a + b + c + d + f));
System.out.println("A: " + a);
System.out.println("B: " + b);
System.out.println("C: " + c);
System.out.println("D: " + d);
System.out.println("F: " + f);
}
Related
I am making a dice game, the rules are 2 dice are thrown randomly, if the sum of both dice are 7 or 11 in the first try, the user wins. if the sum is 2,3 or 12 in the first try, the user loses. if the sum is 4,5,6,8,9,10 then that sum is set as an establish point and continues to throw the dice till the sum matches the established point and the user wins, however if the establish point is 7 the user loses.
the game plays 3 times and records how many times I have won or lost. however, I am not able to get that part finished, help would be appreciated, my code is bellow.
package roll;
import java.util.Random;
public class RandomSumGame {
boolean start;
int d1;
int d2;
int sum = d1 + d2;
int valuepoint;
String plus = "+";
public void play(int d1, int d2) {
int win = 0;
int loss = 0;
sum = d1 + d2;
for (;;) {
if (sum == 2 || sum == 3 || sum == 12) {
start = false;
loss++;
System.out.println(" = " + sum + ";you lose");
break;
} else if (sum == 7 || sum == 11) {
start = true;
win++;
System.out.println(" = " + sum + ";you win ");
break;
}
valuepoint = sum;
if (valuepoint == 4 || valuepoint == 5 || valuepoint == 6 || valuepoint == 8 || valuepoint == 9
|| valuepoint == 10) {
System.out.print(" = " + valuepoint + " you establish a value point " + valuepoint + "\n");
break;
}
}
for (;;) {
if (sum == 7 || sum == 11) {
break;
} else {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
int f1 = d1;
System.out.print("\t" + "-you rolled again " + d1 + " " + plus);
for (int x = 0; x < 1; x++) {
d2 = 1 + rand.nextInt(6);
int f2 = d2;
int loda = f1 + f2;
System.out.print(" " + d2 + " = " + loda + "\n");
}
}
sum = d1 + d2;
if (sum == valuepoint) {
start = true;
win++;
System.out.print("\t" + "you win!" + "\n");
break;
} else if (sum == 7) {
loss++;
start = false;
System.out.print("\t" + "you lose " + "\n");
break;
}
}
}
System.out.print("wins: " + win + "\n");
System.out.print("Losses:" + loss);
}
public void play() {
for (int x = 0; x < 3; x++) {
rolldice();
play(d1, d2);
System.out.print("\n");
}
}
public void rolldice() {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
System.out.print("You rolled " + d1 + " " + plus);
}
for (int i = 0; i < 1; i++) {
d2 = 1 + rand.nextInt(6);
System.out.print(" " + d2 + " ");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
RandomSumGame Teet = new RandomSumGame();
RandomSumGame d1counter = new RandomSumGame();
RandomSumGame d2counter = new RandomSumGame();
Teet.play();
}
}
What part are you not able to finish? Seems like the game is running 3 times? Is it the total number of wins and loses? To fix that, you could just move "int win" and "int lose" outside the play method to make them global variables.
EDIT:
If you don't want to use global variables, you could make a method that calls itself (recursive method)
import java.util.Random;
public class RandomSumGame {
boolean start;
int d1;
int d2;
int sum = d1 + d2;
int valuepoint;
String plus = "+";
public void play(int d1, int d2, int win, int loss) {
sum = d1 + d2;
for (;;) {
if (sum == 2 || sum == 3 || sum == 12) {
start = false;
loss++;
System.out.println(" = " + sum + ";you lose");
break;
} else if (sum == 7 || sum == 11) {
start = true;
win++;
System.out.println(" = " + sum + ";you win ");
break;
}
valuepoint = sum;
if (valuepoint == 4 || valuepoint == 5 || valuepoint == 6 || valuepoint == 8 || valuepoint == 9
|| valuepoint == 10) {
System.out.print(" = " + valuepoint + " you establish a value point " + valuepoint + "\n");
break;
}
}
for (;;) {
if (sum == 7 || sum == 11) {
break;
} else {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
int f1 = d1;
System.out.print("\t" + "-you rolled again " + d1 + " " + plus);
for (int x = 0; x < 1; x++) {
d2 = 1 + rand.nextInt(6);
int f2 = d2;
int loda = f1 + f2;
System.out.print(" " + d2 + " = " + loda + "\n");
}
}
sum = d1 + d2;
if (sum == valuepoint) {
start = true;
win++;
System.out.print("\t" + "you win!" + "\n");
break;
} else if (sum == 7) {
loss++;
start = false;
System.out.print("\t" + "you lose " + "\n");
break;
}
}
}
System.out.print("wins: " + win + "\n");
System.out.print("Losses:" + loss);
if (win < 2 && loss < 2) {
System.out.print("\n");
//Recursive
play(win, loss);
}
}
public void play(int win, int loss) {
rolldice();
play(d1, d2, win, loss);
}
public void rolldice() {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
System.out.print("You rolled " + d1 + " " + plus);
}
for (int i = 0; i < 1; i++) {
d2 = 1 + rand.nextInt(6);
System.out.print(" " + d2 + " ");
}
}
public static void main(String[] args) {
RandomSumGame test = new RandomSumGame();
test.play(0,0);
}
}
import java.util.Scanner;
public class Hw7Pr2 {
public static void main(String[] args) {
int[] grades = { 40, 55, 70, 58 };
System.out.println("best: ");
int best1 = best(grades);
System.out.print(best1);
// Print Grade
System.out.println("Grade: ");
char [] grade = (char[]) best(grades);
for (char i = 0; i < grade.length; i++) {
String output= " ";
output += "Student " + i + " score is " +
grades[i] + " and grade is " + grade + "\n";
}
}
private static char gradeLetter(int[] grades) {
char grade = 0;
for (int i = 0; i < grades.length; i++) {
if (grades[i] >= best(grades) - 10)
grade = 'A';
else if (grades[i] >= best(grades) - 20)
grade = 'B';
else if (grades[i] >= best(grades) - 30)
grade = 'C';
else if (grades[i] >= best(grades) - 40)
grade = 'D';
else
grade = 'F';
}
return grade;
}
public static int best(int[] grades) {
System.out.println("The best scores is: ");
int best = grades[0];
for (int i = 0; i < grades.length; i++) {
if (grades[i] > best)
best = grades[i];
}
return best;
}
}
I am trying to show the output like this
Student 1 score is 40 and grade is C
Student 2 score is 55 and grade is B
Student 3 score is 70 and grade is A
Student 4 score is 58 and grade is B
But I am having problems with printing the gradeLetter method.
Perhaps this is what you are trying to do :
public class Hw7Pr2 {
public static void main(String[] args) {
int[] grades = { 40, 55, 70, 58 };
int best = best(grades);
System.out.println("The best scores is: " + best);
// Print Grade
System.out.println("Grade: ");
for (int i = 1; i <= grades.length; i++) {
char grade = gradeLetter(grades[i-1], grades);
String output = " ";
output += "Student " + i + " score is " + grades[i-1] + " and grade is " + grade + "\n";
System.out.println(output);
}
}
private static char gradeLetter(int grade, int[] grades) {
char charGrade = 0;
if (grade >= best(grades) - 10)
charGrade = 'A';
else if (grade >= best(grades) - 20)
charGrade = 'B';
else if (grade >= best(grades) - 30)
charGrade = 'C';
else if (grade >= best(grades) - 40)
charGrade = 'D';
else
charGrade = 'F';
return charGrade;
}
public static int best(int[] grades) {
int best = grades[0];
for (int i = 1; i < grades.length; i++) { //will save a compare operation
if (grades[i] > best)
best = grades[i];
}
return best;
}
}
char [] grade = (char[]) best(grades);
Its a invalid part in your code. You can not convert an int to char[] array.
Just wanted to show you some other way of looking at it...
import java.util.Scanner;
public class Hw7Pr2 {
public static void main(String[] args) {
// Search the array for min, max and average
int[] grades = {40, 55, 70, 58};
gradeStats(grades);
}
static void gradeStats(int[] array) {
final int A = 90;
final int B = 80;
final int C = 70;
final int D = 60;
int minimumValue = 100;
int maximumValue = 0;
double gradeSum = 0;
// Get max and min grades
for (int i = 0; i < array.length; i++) {
if (array[i] < minimumValue) {
minimumValue = array[i];
}
if (array[i] > maximumValue) {
maximumValue = array[i];
}
}
// Get sum of grades
for (int i = 0; i < array.length; i++) {
gradeSum = gradeSum + array[i];
}
for (int i = 0; i < array.length; i++) {
if (array[i] >= A) {
System.out.println("Grade \"" + array[i] + "\" is an A.");
} else if (array[i] >= B) {
System.out.println("Grade \"" + array[i] + "\" is an B.");
} else if (array[i] >= C) {
System.out.println("Grade \"" + array[i] + "\" is an C.");
} else if (array[i] >= D) {
System.out.println("Grade \"" + array[i] + "\" is an D.");
} else {
System.out.println("Grade \"" + array[i] + "\" is an F.");
}
}
System.out.println("The lowest grade is a " + minimumValue + ".");
System.out.println("The highest grade is a " + maximumValue + ".");
System.out.printf("The sum is %d.\n", (int) gradeSum);
System.out.println("The average is " + gradeSum / array.length + ".");
}
}
I'm working on a simple java code that outputs all factors of a user-inputted number. How do I count and then display the number of factors outputted?
System.out.println("Enter an integer to be factored:");
int d = Stdin.readInt();
System.out.println("The Factors of " + d + " are:");
for(int w = 1; w <= d; w++ ){
if(d % w == 0){
System.out.println(w);
}
}
In the code above, it's the number of integers outputted in 'w' For instance, if the number inputted is 8 and its factors are 1,2,4,8, how do I write a code that says '8 has 4 factors' ?
Thanks
You simply need a variable to count factors:
System.out.println("Enter an integer to be factored:");
int d = Stdin.readInt();
int nFactors = 0;
System.out.println("The Factors of " + d + " are:");
for(int w = 1; w <= d; w++ ){
if(d % w == 0){
System.out.println(w);
++nFactors;
}
}
System.out.println(d + " has " + nFactors + " factors");
You need a counter variable. Here is the code:
int counter =0;
for(int w = 1; w <= d; w++ ){
if(d % w == 0){
counter++;
System.out.println(w);
}
System.out.println(d + " has " + counter + "factors ");
Try with this code:
import java.util.Scanner;
public class EmbalzadoFactorial {
public static Scanner sc;
public static void main(String[] args) {
int Number, i;
sc = new Scanner(System.in);
System.out.print("Please Enter any number to Find Factors: ");
Number = sc.nextInt();
System.out.println("The factors are: ");
for(i = 1; i <= Number; i++) {
if(Number%i == 0) {
System.out.format(" %d ", i);
System.out.print ("and");
System.out.format("%s %n ", i);
}
}
}
}
Ok so I'm supposed to be reading and printing from a text file with the code yet every time I run I get a "java.utilNoSuchElementException" on line 31 "grade = in.nextInt();". The current text file is
2 80 97
5 69 79 89 99 58
7 60 70 80 90 100 0 59
where the first number is the number of scores in each section, each section is supposed to be counted (ie. 1, 2, 3) Anyways one problem at a time. here's the current code.
import java.util.Scanner;
import java.io.*;
public class Prog2
{
public static void main (String args []) throws IOException
{
Scanner in = new Scanner (new File ("test1.txt"));
int Lowest, Highest, grade = 0;
float section_average, class_average;
int count = 0, A = 0, B = 0, C = 0, D = 0, F = 0, total_number_of_sections = 0, total_number_of_scores = 0, number = 0;
while (in.hasNextInt())
{
number = in.nextInt();
System.out.println (in.nextInt());
number ++;
while (count < number)
{
grade = in.nextInt();
total_number_of_sections += number;
total_number_of_scores += grade;
total_number_of_scores ++;
count++;
}
}
if (number > 0)
{
System.out.println ("Scores for section "+count);
}
else
{
System.out.println ("Scores for section 0");
}
if (grade >= 90)
{
A ++;
}
if (grade >= 80 && grade < 90)
{
B ++;
}
if (grade >= 70 && grade < 80)
{
C ++;
}
if (grade >= 60 && grade < 70)
{
D ++;
}
if (grade < 60)
{
F ++;
}
System.out.println (" ");
System.out.println ("Scores for section "+count);
System.out.println ("A's" + A);
System.out.println ("B's" + B);
System.out.println ("C's" + C);
System.out.println ("D's" + D);
System.out.println ("F's" + F);
System.out.println ("Lowest Score: ");
System.out.println ("Highest Score: ");
System.out.println (" ");
System.out.println (" ");
System.out.println ("Total number of sections: " + total_number_of_sections);
System.out.println ("Total number of scores: " + total_number_of_scores);
System.out.println ("Class Average: ");
}
}
try this
while (in.hasNextInt())
{
count = 0;
number = in.nextInt();
System.out.println (in.nextInt());
while (in.hasNextInt())
{
grade = in.nextInt();
total_number_of_sections += number;
total_number_of_scores += grade;
total_number_of_scores ++;
if(++count == number ){ break;}
}
}
Hi I am new in programming,so please don't laugh from my stupid question.
I wrote program which ask user for input a number than program should output all the numbers from 0 to that entered number(doesn't matter if it is positive or negative).
I have tried 3 different versions
Here is my code:
int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
int num = PutiL.validNum(min, max, "number");
//this my utility methode which check if number is in range and if it is not a double or letter
int i = 0, z;
int y = 0;
//3rd version
while (i <= num) {
for (z = 0; z < 4; z++) {
System.out.print(i + " ");
i++;
}
System.out.println();
}
//2nd version
if (num > 0) {
for (i = 0; i <= num; y++) {
for (z = 0; z < 4; z++) {
System.out.print(i + ",");
i++;
}
System.out.println();
}
} else {
for (i = 0; i > num; y--) {
for (z = 0; z < 4; z++) {
System.out.print(i + ",");
i--;
}
System.out.println();
}
}
//1st version`enter code here`
if (num > 0) {
for (i = 0; i <= num; i++)
{
System.out.print(i + ",");
}
} else {
for (i = 0; i >= num; i--) {
System.out.print(i + ",");
}
}
System.out.println();
Problem is that code doesn't stop straight after number typed in by user. Will someone give me a hint what is wrong as I don't have any more ideas.
And here is the PutiL methode
public static int validNum(int min, int max, String words) {
int num;
do {
System.out.println("Please enter " + words);
while (!kb.hasNextInt()) {
System.out.println("Please re-enter ");
kb.nextLine();
}
num = kb.nextInt();
if (num < min || num > max) {
System.out.println("Not in range - re-enter\tproper range is "
+ min + " - " + max);
}
} while (num < min || num > max);
return num;
}
//2nd version
if (num > 0)
{
for (i = 0; i <= num; y++) <== THIS MAKE INFINITE too, OKAY.. ^^, change i to stop
{
for (z = 0; z < 4; z++)
{
System.out.print(i + ",");
i++;
}
System.out.println();
}
} else
{
for (i = 0; i > num; y--) <== THIS MAKE INFINITE LOOPS, OKAY.. ^^, it must i to stop
{
for (z = 0; z < 4; z++)
{
System.out.print(i + ",");
i--;
}
System.out.println();
}
}
don't forget to accepted the answer if it goes right.. ^^
public static void main(String[] args) {
int number = Integer.parseInt(args[0]);
if (number >= 0) {
for (int i = 0; i <= number; i++) {
System.out.println(i);
}
} else {
for (int i = 0; i >= number; i--) {
System.out.println(i);
}
}
}
Or a bit more concise and with duplicating the println statement...
public static void main(String[] args) {
int number = Integer.parseInt(args[0]);
int increment = number >= 0 ? 1 : -1;
for (int i = 0; i != number + increment; i += increment) {
System.out.println(i);
}
}
This will give the user a dialog and the value the user enters will count to zero.
First import:
import javax.swing.JOptionPane.*;
Then:
int user_choice = Integer.parseInt(
showInputDialog(null, "Please enter a number."));
if(user_choice > 0){
for(int temp = 0; temp <= user_choice; temp++){
System.out.println(temp);
}
}
else{
for(int temp = 0; temp >= user_choice; temp--){
System.out.println(temp);
}
}
"program should output all the numbers from 0 to that entered number"
and
"I want them print for example 4 in a one line and than skip to another line"
if(num >=0) {
for (z = 0; z <= num; z++)
{
System.out.print(z + " ");
if(z > 0 && z%4==0)
System.out.println();
}
}
else {
// similar loop for negatives
}
System.out.println();
Thank you all for help I solve it my self was really easy here is code I useit and now its work perfectly
for(i = 0; i <= num; i++)
{
System.out.print(i + " ");
y++;
if(y % 4 == 0)
{
System.out.println();
}
}
for(i = 0; i >= num; i--)
{
System.out.print(i + " ");
y++;
if(y % 4 == 0)
{
System.out.println();
}
}
But thank you all again for giving my ideas.