代码应输出Farenheit和Celsius的表格
public static void main(String[] args) {
System.out.println("Fahrenheit\tCelsius");
System.out.println("=======================");
for(int temp = -45; temp <= 120; temp += 5) //for(int i = 0; i <= 100; i+= 10)
{
System.out.printf("%5d |", temp);
double sum = (temp + (9.0/5.0)) * 32;
System.out.printf("%5d", (int)sum );
System.out.printl
n();
答案 0 :(得分:0)
你应该在一个包含while循环的地方完成这一切。
while循环伪代码是这样的:
string answer = "";
While (answer != quit)
{
answer = (ask question to user)
if (answer != quit)
compute/display response
}
答案 1 :(得分:0)
看起来你的大括号没有正确打开和关闭。此外,每次迭代都会重置userScore和totalScore。所以你没有得到合适的分数。把它移到循环外面。
import java.util.Scanner;
public class Multiplication {
private static String question;
private static Scanner input;
private static Scanner scanner;
private static int totalScore;
private static int answer1;
private static int userScore;
public static void main(String[] args) {
int number1;
int number2;
totalScore = 0;
userScore = 0;
input = new Scanner(System.in);
do {
number1 = (int)(Math.random() * 10);
number2 = (int)(Math.random() * 10);
System.out.print("What is " + number1 + " * " + number2 + "? ");
int answer = input.nextInt();
while (number1 * number2 != answer)
{
int answer1 = number1 * number2;
System.out.println("Incorrect. The answer is " + answer1+".");
System.out.print("Want more questions yes or no? ");
scanner = new Scanner (System.in);
question = scanner.next();
totalScore++;
break;
}
if (number1 * number2 == answer)
{
System.out.println("Correct. Nice work!");
System.out.print("Want more questions yes or no? ");
scanner = new Scanner (System.in);
question = scanner.next();
userScore++;
totalScore++;
}
}while(question.equals("yes") || question.equals("y"));
System.out.println("You have this many questions correct:" + userScore +" out of " + totalScore);
}
}
更好的实施
这里的代码看起来更具可读性,并且删除了一些不必要的代码行。
import java.util.Scanner;
public class Multiplication {
private static final Scanner scanner = new Scanner (System.in);
private static int totalScore = 0;
private static int userScore = 0;
private static int number1;
private static int number2;
private static int answer;
public static boolean continueExe(){
String question;
System.out.print("Want more questions yes or no? ");
question = scanner.next();
return (question.equals("yes") || question.equals("y"));
}
public static void printOutput(){
System.out.println("You have this many questions correct:" + userScore +" out of " + totalScore);
}
public static void askQuestion(){
number1 = (int)(Math.random() * 10);
number2 = (int)(Math.random() * 10);
System.out.print("What is " + number1 + " * " + number2 + "? ");
}
public static void readAnswer(){
answer = scanner.nextInt();
}
public static void isAnswerCorrect(){
if (number1 * number2 != answer){
System.out.println("Incorrect. The answer is " + number1 * number2 +".");
}else{
userScore++;
System.out.println("Correct. Nice work!");
}
totalScore++;
}
public static void main(String[] args) {
do{
askQuestion();
readAnswer();
isAnswerCorrect();
}while(continueExe());
printOutput();
}
}
答案 2 :(得分:0)
错误是你打开'{'两次'if'statement -
if (question.equals("no")||question.equals("n")&& (number1 * number2 == answer)){
{
并忘了在最后一行关闭一个括号。
下面的代码解决了由括号引起的错误:
import java.util.Scanner;
public class Program3_3 {
private static String question;
private static Scanner input;
private static Scanner scanner;
private static int totalScore;
private static int answer1;
private static int userScore;
public static void main(String[] args) {
do {
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
totalScore = 0;
userScore = 0;
input = new Scanner(System.in);
System.out.print("What is " + number1 + " * " + number2 + "? ");
int answer = input.nextInt();
while (number1 * number2 != answer) {
int answer1 = number1 * number2;
System.out.println("Incorrect. The answer is " + answer1+".");
System.out.print("Want more questions yes or no? ");
scanner = new Scanner (System.in);
question = scanner.next();
break;
}
if (number1 * number2 == answer){
System.out.println("Correct. Nice work!");
System.out.print("Want more questions yes or no? ");
scanner = new Scanner (System.in);
question = scanner.next();
}
if (question.equals("no")||question.equals("n")&& (number1 * number2 == answer))
{
userScore++;
totalScore++;
System.out.println("You have this many questions correct:" + totalScore);
}
}
while(question.equals("yes") || question.equals("y"));
}
}