-3
所以我正在學習JAVA,而且我一般只掌握編程語言的基本知識。我寫了這個簡單的計算器程序來嘗試應用我迄今爲止學到的東西,但問題在於它不打印年齡,而是打印出0,我不知道爲什麼:Java get()函數不起作用
this是類
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!");
}
}
}
忘記'sql'和'javascript'標籤。 –
爲什麼在上帝的名字你有標籤C#和C的Java問題? – smoggers
我不認爲你的代碼可以編譯。爲什麼不告訴我們一個可執行的代碼,並告訴我們您的確切輸入,預期和實際輸出。你有一個名爲'userInput'的類,帶有'tuna'構造函數。 – user3437460