Eclipse不显示多个方法的输出?

时间:2018-05-14 18:05:33

标签: java eclipse console

作为一个相当新的程序员和刚开始学习Java的人。我认为什么是比计算器更好的项目?我遇到的问题是打印主方法之外的输出,如下图所示。

import java.util.Scanner;

public class Calculator extends Calculator_Assets {

public static void main(String[] args) {
    System.out.println("Choose One!");

    System.out.println("1. Addition\n");

    System.out.println("2. Subtraction\n");

    System.out.println("3. Multiplication\n");

    System.out.println("4. Division\n");

    //prints out the users input, not their mathematical selection
String word = newInput.nextLine();
if (word.equals("1")) 
        System.out.println("You have chosen Addition!");


else if (word.equals("2"))          
        System.out.println("You have chosen Subtraction!");
else if (word.equals("3"))
        System.out.println("You have chosen Multiplication!");
else if (word.equals("4"))
        System.out.println("You have chosen Division!");
    //Break here, to input integers




}
public static void operands() {
    String num1 = newInput.nextLine();
    String num2 = newInput.nextLine();
    System.out.println("Enter two numbers to add:");
}
}   

输出“输入两个要添加的数字:”不会出现在控制台中。

PS:如何改进我的代码结构以使其看起来更整洁?什么是行业标准?

2 个答案:

答案 0 :(得分:0)

首先,我没有看到你在main中调用方法。 操作数(); //将调用方法

其次,你可能想要取字符串" word"方法。 使用它代替operands();

operands(word); //this says "run method operands, and make the stored variable word also useful for this new method"

然后代替public static void operands(){

public static void operands(String word) { //we have the input word from our main, and now in the parantheses we can put how we would like to call this variable (and declaring it) , you can name it different than in the main 

答案 1 :(得分:-1)

对你的改进可能就是这样。代码仍然不是很好(因为我已经使用了2次,如果(选择== 1/2/3/4),而那基本上不需要(你可以把它放在一次)但是为了保持你的结构我这样做了。

import java.util.*;


class test{
static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {

    System.out.println("Choose\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n");
    int choose = sc.nextInt();



    if (choose == 1) 
    { 

            System.out.println("You have chosen Addition!");
    }

    if (choose == 2) 
    {      

            System.out.println("You have chosen Subtraction!");
    }

    if (choose == 3)
    {

            System.out.println("You have chosen Multiplication!");
    }

    if (choose == 4)
    {

            System.out.println("You have chosen Division!");
    }

    operands(choose);

}

public static void operands(int choose) {
    System.out.println("Enter two numbers to add:");
    int number1 = sc.nextInt();
    int number2 = sc.nextInt();

    if (choose == 1) { 

            int answer = number1 + number2;
            System.out.println(answer);
    }

    if (choose == 2)   {    
            int answer = number1 - number2;
            System.out.println(answer);
    }

    if (choose == 3) {
            int answer = number1 * number2;
            System.out.println(answer);
    }

    if (choose == 4) {
            int answer = number1 / number2;
            System.out.println(answer);
    }
}   

}