这是我到目前为止它不起作用我不知道为什么哈哈。 我想我是以正确的方式做到了,但为什么
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package midterm;
import java.util.Scanner;
/**
*
* @author prvillanuevajr
*/
public class Calc {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int x = 0;
int y = 0;
Myarithmetic m = new Myarithmetic();
System.out.println("[1] - Add");
System.out.println("[2] - Subtract");
System.out.println("[3] - Multiply");
System.out.println("[4] - Divide");
System.out.print("CHOOSE: ");
int choose = s.nextInt();
switch (choose) {
case 1:
System.out.println("ADDITION");
x = s.nextInt();
y = s.nextInt();
m.setX(x);
m.setY(y);
m.add();
break;
default:
System.out.println("INVALID INPUT");
}
}
}
public class Myarithmetic {
private int y;
private int x;
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int add(){
int sum = 0;
sum = getX()+getY();
return sum;
}
}
答案 0 :(得分:1)
那是因为您在同一源文件中编写两个公共类删除public或将第二个文件写入新的.java文件
否则,我会在您的程序中向您显示一些修改,以便根据您的需要进行操作:
public class A {
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
int x,y;
System.out.println("1.add 2.subtract");
System.out.println("Enter your choice: ");
int choice = sc.nextInt();
A obj = new A();
switch(choice){
case 1:
System.out.println("Enter two number to add: ");
x=sc.nextInt();
y=sc.nextInt();
obj.add(x,y);
break;
case 2:
/* Case 2 containts here */
}
}
public void add(int x, int y){
int a=x,b=y;
int answer = a+b;
System.out.println("Addition is :"+answer);
}
}
答案 1 :(得分:0)
您实际上并未打印结果;你刚刚调用add()
方法。
尝试:
System.out.println(m.add());