Java get()函数不起作用

时间:2016-12-17 11:08:46

标签: java

所以我自己学习JAVA,而且我只掌握有关编程语言的基本知识。我写了这个简单的计算器程序来尝试应用我迄今为止学到的东西,但问题是它没有打印时代,而是打印出0并且我不知道为什么:

这是班级

public class userinput {

private String name;
private int age;

public tuna (String name, int age){
    name = "dina";
    age = 3;
}

public void simpleMessage2(){
    System.out.println("hello " + name + " ready to use our calculator?");
}

public void setName(String Uname){
    name = Uname;
}

public void setAge(int uage){
    uage = age;
}

public String getName(){
    return name;
}

public int getAge(){
    return age;
}

public void printname(){
    System.out.printf("your name is %s", getName());
    System.out.println();
}

public void printage(){
    System.out.println("your age is");
    System.out.println(getAge());
    System.out.println();
}}

这是主要的课程:

import java.util.Scanner;

class calc {
public static void main (String args[]) {

    String name1;
    int age1;

    Scanner bucky = new Scanner(System.in);
    int choice, num1, num2, sum;

    System.out.println("Hey, enter your name");
    name1 = bucky.nextLine();
    System.out.println("Hey, enter your age");
    age1 = bucky.nextInt();

    tuna objc1 = new userinput(name1, age1);

    objc1.setName(name1);
    objc1.printname();
    System.out.println();

    objc1.setAge(age1);
    objc1.printage();
    System.out.println();

    System.out.println("this is a basic calculator, select from the menu:");
    System.out.println("Enter 1 for summation");
    System.out.println("Enter 2 for subtraction");
    System.out.println("Enter 3 for multiplication");
    System.out.println("Enter 4 for division");
    System.out.println("Enter 5 for module");
    System.out.println("Enter 0 to exit");

    choice = bucky.nextInt();

    while (choice != 0) {   

        switch(choice){

        case 1:
            System.out.println("enter the 1st num");
            num1 = bucky.nextInt();
            System.out.println("enter the 2nd num");
            num2 = bucky.nextInt();
            System.out.println("the sum is equal to: ");
            sum = num1 + num2;
            System.out.print(sum);
            System.out.println("Select another operation from the menu or enter 0 to exit");
            choice = bucky.nextInt();
            break;

        case 2:
            System.out.println("enter the 1st num");
            num1 = bucky.nextInt();
            System.out.println("enter the 2nd num");
            num2 = bucky.nextInt();
            System.out.println("the sub is equal to: ");
            sum = num1 - num2;
            System.out.print(sum);
            System.out.println("Select another operation from the menu or enter 0 to exit");
            choice = bucky.nextInt();
            break;

        case 3:
            System.out.println("enter the 1st num");
            num1 = bucky.nextInt();
            System.out.println("enter the 2nd num");
            num2 = bucky.nextInt();
            System.out.println("the mul is equal to: ");
            sum = num1 * num2;
            System.out.print(sum);
            System.out.println("Select another operation from the menu or enter 0 to exit");
            choice = bucky.nextInt();
            break;

        case 4:
            System.out.println("enter the 1st num");
            num1 = bucky.nextInt();
            System.out.println("enter the 2nd num");
            num2 = bucky.nextInt();
            System.out.println("the div is equal to: ");
            sum = num1 / num2;
            System.out.print(sum);
            System.out.println("Select another operation from the menu or enter 0 to exit");
            choice = bucky.nextInt();
            break;

        case 5:
            System.out.println("enter the 1st num");
            num1 = bucky.nextInt();
            System.out.println("enter the 2nd num");
            num2 = bucky.nextInt();
            System.out.println("the mod is equal to: ");
            sum = num1 % num2;
            System.out.print(sum);
            System.out.println("Select another operation from the menu or enter 0 to exit");
            choice = bucky.nextInt();
            break;

        default:
            System.out.println("Invalid entry, please try again");
            choice = bucky.nextInt();
            break;
        }

        System.out.println("Bye!");
    }
}
}

2 个答案:

答案 0 :(得分:0)

金枪鱼类在哪里?

tuna objc1 = new userinput(name1, age1);

userinput的构造函数:

public userinput(String n, int a){
    name = n;
    age = a;
}

我刚刚学习

答案 1 :(得分:0)

您没有将金枪鱼类更改为

的第一个错误
tuna objc1 = new userinput(name1,age1);

userinput objc1 = new userinput();

然后我改变你的代码就像你在头等舱中使用它的方法一样 和第二类进行操作 第一堂课

import java.util.Scanner;

public class userinput {

private  String name;
private int age;

public void simpleMessage2(){
    System.out.println("hello " + name + " ready to use our calculator?");
}

public void setName(String Uname){
    name = Uname;
}

public void setAge(int uage){
        age= uage;    }
public void printname(){
    System.out.printf("your name is %s", getName());
    System.out.println();
}
public void printage(){
System.out.printf("your age is %s", getAge());
    System.out.println();}
public int getAge(){
    return age;
}
public String getName(){
    return name;
}
}

第二课

class calc {
public static void main (String args[]) {

    String name1;
    int age1;

    Scanner bucky = new Scanner(System.in);
    int choice, num1, num2, sum;

    System.out.println("Hey, enter your name");
    name1 = bucky.nextLine();
    System.out.println("Hey, enter your age");
    age1 = bucky.nextInt();

    userinput objc1 = new userinput();

    objc1.setName(name1);
    objc1.printname();
    System.out.println();

    objc1.setAge(age1);
    objc1.printage();
    System.out.println();
    objc1.simpleMessage2();
    System.out.println();

    System.out.println("this is a basic calculator, select from the menu:");
    System.out.println("Enter 1 for summation");
    System.out.println("Enter 2 for subtraction");
    System.out.println("Enter 3 for multiplication");
    System.out.println("Enter 4 for division");
    System.out.println("Enter 5 for module");
    System.out.println("Enter 0 to exit");

    choice = bucky.nextInt();

    while (choice != 0) {   

        switch(choice){

        case 1:
            System.out.println("enter the 1st num");
            num1 = bucky.nextInt();
            System.out.println("enter the 2nd num");
            num2 = bucky.nextInt();
            System.out.println("the sum is equal to: ");
            sum = num1 + num2;
            System.out.println(sum);
            System.out.println("Select another operation from the menu or enter 0 to exit");
            choice = bucky.nextInt();
            break;

        case 2:
            System.out.println("enter the 1st num");
            num1 = bucky.nextInt();
            System.out.println("enter the 2nd num");
            num2 = bucky.nextInt();
            System.out.println("the sub is equal to: ");
            sum = num1 - num2;
            System.out.println(sum);
            System.out.println("Select another operation from the menu or enter 0 to exit");
            choice = bucky.nextInt();
            break;

        case 3:
            System.out.println("enter the 1st num");
            num1 = bucky.nextInt();
            System.out.println("enter the 2nd num");
            num2 = bucky.nextInt();
            System.out.println("the mul is equal to: ");
            sum = num1 * num2;
            System.out.println(sum);
            System.out.println("Select another operation from the menu or enter 0 to exit");
            choice = bucky.nextInt();
            break;

        case 4:
            System.out.println("enter the 1st num");
            num1 = bucky.nextInt();
            System.out.println("enter the 2nd num");
            num2 = bucky.nextInt();
            System.out.println("the div is equal to: ");
            sum = num1 / num2;
            System.out.println(sum);
            System.out.println("Select another operation from the menu or enter 0 to exit");
            choice = bucky.nextInt();
            break;

        case 5:
            System.out.println("enter the 1st num");
            num1 = bucky.nextInt();
            System.out.println("enter the 2nd num");
            num2 = bucky.nextInt();
            System.out.println("the mod is equal to: ");
            sum = num1 % num2;
            System.out.println(sum);
            System.out.println("Select another operation from the menu or enter 0 to exit");
            choice = bucky.nextInt();
            break;

        default:
            System.out.println("Invalid entry, please try again");
            choice = bucky.nextInt();
            break;
        }


    }
    System.out.println("Bye!");}


}

我希望我能帮到你