Hello, so I am creating a program in netbeans that gives a 10 question quiz and
ID: 3701726 • Letter: H
Question
Hello, so I am creating a program in netbeans that gives a 10 question quiz and calculates how many are right or wrong and tells the student what his or her grade is afterwards. I've figured out most of it except the variable for the number of correct answers being printed in the output correctly. When I tried to make the variable numCorrect add one whenever the user gets it right, I print the variable afterwards and it still says zero
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package assignment4;
import static java.lang.System.out;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Harper
*/
public class Assignment4 {
// standard final static variables that all methods can share
final static Scanner cin = new Scanner(System.in);
final static Random rand = new Random();
public static void main(String[] args) {
// output identification
int x=1, numProbs=10, numCorrect=0, probNum=1;
out.print("CPS 150 Assignment 4 by Harper Miller ");
out.println();
out.println("Welcome to the Addition tutor program");
out.print("Enter your Given name (no spaces):");
String name=cin.nextLine();
out.println();
out.println("Greetings, " +name + "!");
out.println("I will ask you 10 Problems");
out.println("Ready, set, go!");
while (10>=x){
oneProblem(numCorrect , probNum);
probNum+=1;
x+=1;}
// NEED TO ADD THINGS HERE (THE DEFINITE LOOP SHOULD BE HERE!)
out.print(numCorrect);
out.print(" Assignment 4 complete ");
} // end main
// function to present one problem to the learner
static int oneProblem(int numCorrect, int probNum) {
int z , y , answer;
z=10+rand.nextInt(90);
y=10+rand.nextInt(90);
out.print("Problem " + probNum + ": " + z + " + " + y + "= ? ");
answer=cin.nextInt();
if (z+y == answer){
numCorrect++;
out.println("Your answer is correct!");}
else{
answer= z+y;
out.println("your answer is wrong. The correct answer is:" + answer);}
return numCorrect ; // to let the program compile
} // end oneproblem
static void report(String name, int numProbs, int numCorrect) {
// NEED TO ADD THINGS HERE
} // end report
static char getLetterGrade(int numProbs, int numCorrect) {
// NEED TO ADD THINGS HERE
return 'X'; // to let the program compile
} // end getLetterGrade
}
Explanation / Answer
Hi friend, In Java , primitive variables are always passed in methods by PASS BY VALUE. So if you change a variable in a method, it will not affect the original value stored in the variable.
Here you can declare static variable, now you can use this at any place in your code.
Please rate my answer of it helped you!!
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import static java.lang.System.out;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Harper
*/
public class Assignment4 {
// standard final static variables that all methods can share
final static Scanner cin = new Scanner(System.in);
final static Random rand = new Random();
private static int numCorrect = 0;
public static void main(String[] args) {
// output identification
int x=1, numProbs=10, probNum=1;
out.print("CPS 150 Assignment 4 by Harper Miller ");
out.println();
out.println("Welcome to the Addition tuto program");
out.print("Enter your Given name (no spaces):");
String name=cin.nextLine();
out.println();
out.println("Greetings, " +name + "!");
out.println("I will ask you 10 Problems");
out.println("Ready, set, go!");
while (10>=x){
oneProblem(probNum);
probNum+=1;
x+=1;}
// NEED TO ADD THINGS HERE (THE DEFINITE LOOP SHOULD BE HERE!)
out.print(numCorrect);
out.print(" Assignment 4 complete ");
} // end main
// function to present one problem to the learner
static int oneProblem(int probNum) {
int z , y , answer;
z=10+rand.nextInt(90);
y=10+rand.nextInt(90);
out.print("Problem " + probNum + ": " + z + " + " + y + "= ? ");
answer=cin.nextInt();
if (z+y == answer){
numCorrect++;
out.println("Your answer is correct!");}
else{
answer= z+y;
out.println("your answer is wrong. The correct answer is:" + answer);}
return numCorrect ; // to let the program compile
} // end oneproblem
static void report(String name, int numProbs) {
// NEED TO ADD THINGS HERE
} // end report
static char getLetterGrade(int numProbs) {
// NEED TO ADD THINGS HERE
return 'X'; // to let the program compile
} // end getLetterGrade
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.