-2

我想问一下我的 java 代码,如何从方法返回 int 值到 main() 以从 main() 显示为打印,这里是我编写但无法从 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新手,希望能帮我举一些例子或建议

4

1 回答 1

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);
于 2013-09-09T19:34:25.180 回答