我想问一下我的java代码,如何将int值从方法返回到main(),以便从main()显示为print,这里是我写的代码,但无法从main()打印:
package salaryemp;
import java.util.Scanner;
public class SalaryEmp {
int hour_rate, normal, overt, t_normal, t_overt, all_t, otpr;
SalaryEmp(int x,int z, int r){
hour_rate = x;
normal = z;
overt = r;
}
void Weekcount(){
otpr = (hour_rate/2)+hour_rate;
t_normal = normal*hour_rate;
t_overt = overt*otpr;
all_t = (t_normal + t_overt);
//Dont want print from this method, i want print from main()
//System.out.println("Your salary for this week is RM: " + t_normal );
//System.out.println("Your salary for this week is RM: " + t_overt );
//System.out.println("Your salary for this week is RM: " + all_t );
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int hourrate, tworkingh, tovertt;
System.out.print("Insert hourly rate RM: ");
hourrate = input.nextInt();
System.out.print("Insert Total working hour : ");
tworkingh = input.nextInt();
System.out.print("Insert Total overtime hour : ");
tovertt = input.nextInt();
SalaryEmp s1 = new SalaryEmp(hourrate, tworkingh, tovertt);
s1.Weekcount();
//*Print result here
}
}
我希望java中的新手,可以帮我提供一些示例或建议
答案 0 :(得分:3)
只需将Weekcount()
返回类型设为int
int Weekcount()
{
otpr = (hour_rate/2)+hour_rate;
t_normal = normal*hour_rate;
t_overt = overt*otpr;
return (t_normal + t_overt);
}
并从main()
这样调用它。
int weekCount = s1.Weekcount();
System.out.println(weekCount);