Java不返回乘法双精度
我需要计算用户答对的问题的百分比。双右是用户正确回答的许多问题。由于某些原因,当用户正确回答问题时,它不会将更新后的right值返回给main方法。我有所有的数学知识,程序运行正常。我从终端得到的唯一回应是“你得到了0%的正确率。”Java不返回乘法双精度,java,Java,我需要计算用户答对的问题的百分比。双右是用户正确回答的许多问题。由于某些原因,当用户正确回答问题时,它不会将更新后的right值返回给main方法。我有所有的数学知识,程序运行正常。我从终端得到的唯一回应是“你得到了0%的正确率。” import java.util.Scanner; 导入java.util.Random; 公共课程{ 公共静态void main(字符串[]args){ System.out.println(“您想问多少问题?”); 扫描仪sc=新的扫描仪(System.in);
import java.util.Scanner;
导入java.util.Random;
公共课程{
公共静态void main(字符串[]args){
System.out.println(“您想问多少问题?”);
扫描仪sc=新的扫描仪(System.in);
双数;
num=sc.nextDouble();
双重问题;
双重权利;
右=0.0;
问题=0.0;
双重问题;
提问=提问;
而(questionsimport java.util.Scanner;
import java.util.Random;
public class program {
public static void main(String[] args) {
System.out.println("How Many Questions Would You Like?");
Scanner sc = new Scanner(System.in);
Double num;
num = sc.nextDouble();
Double questions;
Double right;
right = 0.0;
questions = 0.0;
Double questionsasked;
questionsasked = questions;
while (questions < num) { // Compares Initial value of questions to num,is how many questions that the
// user wants.
// Ask User What Type Of Problem
System.out.println("What Kind Of Problem Would You Like?");
// 1 for Addition, 2 for Subtraction, 3 For Multiplication, 4 for Division
System.out.println("1 for \"+\", 2 for \"-\", 3 for \"*\", 4 for \"/\"");
// Takes Input Values For Problem Type
Scanner sd = new Scanner(System.in);
Double input;
input = sc.nextDouble();
// Register What Problem They Would Like
// Input is what type of problem user wants
if (input == 1) {
// Addition Problem, Passes Number Of Questions Right, and other necessary
// things
// Adds Value To Number Of Questions Asked, Used To Find Percentage Correct
questions = questions + 1.0;
// Calls Method
add(right, questions, questionsasked);
} else if (input == 2) {
// Subtraction Problem, Passes Number Of Questions Right, and other necessary
// things
// Adds Value To Number Of Questions Asked, Used To Find Percentage Correct
questions = questions + 1.0;
// Calls Method
subtract(right, questions, questionsasked);
} else if (input == 3) {
// Multiply Problem, Passes Number Of Questions Right, and other necessary
// things
// Adds Value To Number Of Questions Asked, Used To Find Percentage Correct
questions = questions + 1.0;
// Calls Method
multiply(right, questions, questionsasked);
} else if (input == 4) {
// Division Problem, Passes Number Of Questions Right, and other necessary
// things
// Adds Value To Number Of Questions Asked, Used To Find Percentage Correct
questions = questions + 1.0;
// Calls Method
divide(right, questions, questionsasked);
} else {
// If Answer to what type of problem they want is not 1-4 it will print this.
System.out.println("Not A Valid Problem.");
}
}
Double calcper;
calcper = right / questions;
Double calcperfinal;
calcperfinal = calcper * 100;
System.out.println("You Got " + calcperfinal + "% Correct");
System.out.println("Program Is Done Running!"); // This Is The Very Last Thing That Happens.
}
// Addition Method
public static Double add(Double right, Double questions, Double questionsasked) {
System.out.println("OK, Let's Add!");
Random rand = new Random(); // This Declares The Random Number
Double rnumber = rand.nextInt((int) 100.0) + 0.0; // 100 Is The Max, 0 Is The Min.
Random rand2 = new Random(); // This Declares The Random Number
Double rnumber2 = rand2.nextInt((int) 100.0) + 0.0; // 100 Is The Max, 0 Is The Min.
System.out.println("What Is, " + rnumber + " + " + rnumber2 + "?"); // This Will Ask Question.
Scanner sc = new Scanner(System.in);
Double add; // This Is The User Answer
add = sc.nextDouble(); // This Sets Current Scanner To The User Answer
int answer = (int) (rnumber + rnumber2); // Creates INT To Determine The Right Answer
if (add == answer) { // Compares User Answer To The Correct Computer Answer
System.out.println("Correct!"); // Prints Out Correct
right = right + 1.0;
return right;
// Used If user Got Question Incorrect
} else {
System.out.println("No, the correct answer is " + answer); // Prints The Correct Answer.
return right;
}
}
// Subtraction Method
public static Double subtract(Double right, Double questions, Double questionsasked) {
System.out.println("OK, Let's Subtract!");
Random rand = new Random(); // This Declares The Random Number
Double rnumber = (double) (rand.nextInt(100) + 0.0); // 100 Is The Max, 0 Is The Min.
Random rand2 = new Random(); // This Declares The Random Number
Double rnumber2 = (double) (rand2.nextInt(100) + 0.0); // 100 Is The Max, 0 Is The Min.
System.out.println("What Is, " + rnumber + " - " + rnumber2 + "?"); // This Will Ask Question.
Scanner sc = new Scanner(System.in);
Double sub; // This Is The User Answer
sub = sc.nextDouble(); // This Sets Current Scanner To The User Answer
int answer = (int) (rnumber - rnumber2); // Creates INT To Determine The Right Answer
if (sub == answer) { // Compares User Answer To The Correct Computer Answer
System.out.println("Correct!"); // Prints Out Correct
right = right + 1;
return right;
// Used If User Gets Question Incorrect
} else {
System.out.println("No, the correct answer is " + answer); // Prints The Correct Answer.
if (questionsasked == 1.0 || questions == 1.0 && right == 0.0) {
System.out.println("You Got %0 Right.");
} else {
return right;
}
}
return questions; // Updates Value Of Questions Asked
}
// Multiplication Method
public static Double multiply(Double right, Double questions, Double questionsasked) {
System.out.println("OK, Let's Multiply!");
Random rand = new Random(); // This Declares The Random Number
Double rnumber = (double) (rand.nextInt(100) + 0); // 100 Is The Max, 0 Is The Min.
Random rand2 = new Random(); // This Declares The Random Number
Double rnumber2 = (double) (rand2.nextInt(100) + 0); // 100 Is The Max, 0 Is The Min.
System.out.println("What Is, " + rnumber + " * " + rnumber2 + "?"); // This Will Ask Question.
Scanner sc = new Scanner(System.in);
Double mul; // This Is The User Answer
mul = sc.nextDouble(); // This Sets Current Scanner To The User Answer
int answer = (int) (rnumber * rnumber2); // Creates INT To Determine The Right Answer
if (mul == answer) { // Compares User Answer To The Correct Computer Answer
System.out.println("Correct!"); // Prints Out Correct
right = right + 1.0;
// Used If User Gets Question Incorrect
} else {
System.out.println("No, the correct answer is " + answer); // Prints The Correct Answer.
return right;
}
return questions; // Updates Value Of Questions Asked
}
// Division Method
public static Double divide(Double right, Double questions, Double questionsasked) {
System.out.println("OK, Let's Divide!");
Random rand = new Random(); // This Declares The Random Number
Double rnumber = (double) (rand.nextInt(100) + 0.0); // 100 Is The Max, 0 Is The Min.
Random rand2 = new Random(); // This Declares The Random Number
Double rnumber2 = (double) rand2.nextInt(100) + 0.0; // 100 Is The Max, 0 Is The Min.
System.out.println("What Is, " + rnumber + " / " + rnumber2 + "?" + ", Round To The Nearest 100th Place."); // This
// Will
// Ask
Scanner sc = new Scanner(System.in);
Double div; // This Is The User Answer
div = sc.nextDouble(); // This Sets Current Scanner To The User
double answer = (rnumber / rnumber2); // Creates Double To Determine
double answerrounded = Math.round(answer * 100.0) / 100.0; // Creates
if (div == answerrounded) { // Compares User Answer To The Correct
System.out.println("Correct!"); // Prints Out Correct
right = right + 1.0;
return right;
// used If User Gets Question Incorrect
} else {
System.out.println("No, the correct answer is " + answerrounded);
}
return questions; // Updates Value Of Questions Asked
}
}
right = add(right, questions, questionsasked);
...
right = subtract(right, questions, questionsasked);
...
right = multiply(right, questions, questionsasked);
...
right = divide(right, questions, questionsasked);
public static Double add(Double right, Double questions, Double questionsasked)
right = add(right, questions, questionsasked);
while (questions < num) // bad practice
Double num;
num = sc.nextDouble();
switch (input)
{
case 1:
{
...
break;
}
...
}
questions = questions + 1.0;
++questions;
right = add(right, questions, questionsasked);