如何从if else语句中取值

时间:2014-01-29 20:49:17

标签: java if-statement

目前我正在尝试构建一个程序,通过设置操作生成随机数的测验,并要求您解决以获得积分。我用if else语句设置每个问题,其中正确答案给出25而错误给出0.但是当我尝试从if else语句外部添加这些数字时,程序无法识别它们已经设置了值。任何帮助都会得到解释,这是我到目前为止的代码的成绩单 包osu.cse1223;

import java.util.Scanner;
import java.util.Random;

public class Project03a {


public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your name");
String Name = keyboard.next();
System.out.println("Hello " + Name + "!");
int upper = 100;
int lower = 10;
int i = (int) ((Math.random() * (upper - lower)) + lower);
System.out.println(i);
int a = (int) ((Math.random() * (upper - lower)) + lower);
int b = (int) ((Math.random() * (upper - lower)) + lower);
int ab = a + b;
int c = (int) ((Math.random() * (upper - lower)) + lower);
int d = (int) ((Math.random() * (upper - lower)) + lower);
int cd = c - d;
int e = (int) ((Math.random() * (upper - lower)) + lower);
int f = (int) ((Math.random() * (upper - lower)) + lower);
int ef = e/f;
int g = (int) ((Math.random() * (upper - lower)) + lower);
int h = (int) ((Math.random() * (upper - lower)) + lower);
int gh = g%h;
int finala = 0;
if (i%2 ==  0) {
    System.out.println("quiz one");
    System.out.println("Question 1: " + a + "+" + b);
    int answera = keyboard.nextInt();
        if (answera == ab) {
            System.out.println("Correct");
            int totala = 25;
            int finalb = finala + totala;
            System.out.println(finalb);
        } else {
            System.out.println("Incorrect");
            int totala = 0;
        }

        System.out.println("Question 2: " + c + "-" + d);
        int answerb = keyboard.nextInt();
            if (answerb == cd) {
                System.out.println("Correct!");
                int totalb = 25;
            }else{
                System.out.println("Incorrect");
            }
        System.out.println("Question 3:" + e + "/" + f);
            int answerc = keyboard.nextInt();
              if (answerc == ef){
                  System.out.println("Correct!");
                  int totalc = 25;
              }else{
                  System.out.println("Incorrect :(");
              }
        System.out.println("Question 4:" + g + "%" + h);
        int answerd = keyboard.nextInt();
            if (answerd == gh){
                 System.out.println("Correct!");
                  int totald = 25;
            }else{ System.out.println("Incorrect :(");
            }
            int score = totala + totalb + totalc +totald;
    }else{
    System.out.println("quiz two");

}

4 个答案:

答案 0 :(得分:3)

此处,totalatotalbtotalctotald在本地声明。声明变量时,它可以在块内访问,而不是在外部。

有两种方式:

您可以在测验开始之前声明并初始化totalatotalbtotalctotald(与a处于同一级别,{ {1}},b)。

您可以保留变量c(并找到更好的名称,如finala),并在4个块中使用它来添加结果。

答案 1 :(得分:1)

这样的宣言
int totala = 0;

声明该变量仅用于 在其所包含的最小范围内,通常是最近的花括号{ .. }。所以这里:

else {
    System.out.println("Incorrect");
    int totala = 0;
}

totala只能在这两个大括号中使用,如果在其他范围内声明int totala,则完全是不同的变量。

要声明可以在多个地方使用的totala,请声明

int totala;

main方法中,但之外的任何其他花括号。然后在分配时忽略单词int

else {
    System.out.println("Incorrect");
    totala = 0;  // will set the totala that you declared outside
}

P.S。:虽然最小的范围通常是最接近的花括号,但for语句的范围不同:

for (int i = 0; i < something; i++) some-statement;

for (int i = 0; i < something; i++) { ... }

int i声明i可以在for的带括号的第一部分内部使用,并在您正在循环的语句或语句组中使用,但在其他任何地方。还有其他特殊情况。)

答案 2 :(得分:0)

使用其余变量初始化totala和finalb,并在if语句中对它们进行操作。

答案 3 :(得分:0)

代码问题与变量范围有关。

例如,在if中声明变量时。该变量仅存在于if。

的范围内
if(answer == ab)
{
  int i = 0;

}

变量i只存在于if循环中。