【问题标题】:Use of Private static私有静态的使用
【发布时间】:2017-10-13 23:56:24
【问题描述】:

你能找到错误的根源吗?

package calc;

import java.util.Scanner;

public class Calc {



    Scanner scan = new Scanner(System.in);

    public void add() {
        System.out.println("Enter 1st number");
        int s1 = scan.nextInt();
        scan.nextLine();
        System.out.println("Enter 2nd number");
        int s2 = scan.nextInt();
        scan.nextLine();

        int sum = s1 + s2;
        System.out.println("The sum is: " + sum);

    }

    public void diff() {
        System.out.println("Enter 1st number");
        int d1 = scan.nextInt();
        scan.nextLine();
        System.out.println("Enter 2nd number");
        int d2 = scan.nextInt();
        scan.nextLine();

        int diff = d1 - d2;
        System.out.println("The difference is: " + diff);

    }

    public void prod() {
        System.out.println("Enter 1st number");
        int p1 = scan.nextInt();
        scan.nextLine();
        System.out.println("Enter 2nd number");
        int p2 = scan.nextInt();
        scan.nextLine();

        int prod = p1 + p2;
        System.out.println("The product is: " + prod);
    }

    public void quo() {
        System.out.println("Enter 1st number");
        int q1 = scan.nextInt();
        scan.nextLine();
        System.out.println("Enter 2nd number");
        int q2 = scan.nextInt();
        scan.nextLine();

        int quo = q1 + q2;
        System.out.println("The quotient is: " + quo);
    }

    public static void main(String[] args) {
        do {

            Calc op = new Calc();
            Scanner scan = new Scanner(System.in);
            char ans = 0;

            System.out.println("Calculator");
            System.out.println("1.Addition\n" + "2.Subtraction\n" + "3.Multiplication\n" + "4.Division\n" + "Enter operation number:");
            int n1 = scan.nextInt();
            scan.nextLine();

            switch (n1) {
                case 1:
                    op.add();
                    break;
                case 2:
                    op.diff();
                    break;
                case 3:
                    op.prod();
                    break;
                case 4:
                    op.quo();
                    break;
                default:
                    System.out.println("Invalid input");
                    break;
            }

            System.out.println("Try again? [Y/N]");
            ans = scan.nextLine().charAt(0);

        } while (ans == 'Y' || ans == 'y');
    }

}

然后 netbeans 自动更正,结果如下:

package calc;

import java.util.Scanner;

public class Calc {

    private static char ans;

它添加了一个“私有静态字符 ans;”我想更多地了解它是如何修复我的代码的。谢谢

【问题讨论】:

  • 如果你在你的do-while循环中声明ans,你不能在while的条件下使用它,它超出了范围。
  • 更好的解决方案是删除static 字段,而是将变量声明为main 方法的本地变量:char ans
  • 在做之前声明char ans = 0; while
  • 请考虑stackoverflow.com/help/mcveminimal 指出:…Minimal – Use as little code as possible that still produces the same problem
  • 对不起,但答案更像是:了解有效的 java 语法是什么样的。而不是仅仅输入内容,然后让您的 IDE 为您修复它;然后把这个超级基本的东西变成一个关于 SO 的问题。

标签: java static private


【解决方案1】:

ansdo{ ... } while() 循环内定义,但必须在外部定义,以使其可用于while 中的条件。

这样做:

char ans = 0;
do {
    Calc op = new Calc();
    Scanner scan = new Scanner(System.in);
    ans = 0;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多