如何在print语句中组合字符串和整数?

时间:2013-11-26 03:07:40

标签: java

我刚开始在大学课程中学习Java。如果/然后声明尚未涵盖,但我想我可以自己尝试。我最终得到了一些语法错误,我认为我修复了,但现在看来当我尝试添加一个带整数的字符串时会出现操作数错误。

我班上的问题是询问用户他们的名字,小时工资率以及他们工作的小时数。我知道教授很快就会要求我们询问用户是否加班加点。如果他们这样做,请询问加班多少小时。然后计算总付款,任何加班费是正常小时费率的1.5倍。

但是我在最后3个print语句中出错,我认为这是因为我试图组合一个字符串和一个整数。我该如何解决?

package javaapplication2;

import java.util.Scanner;

public class JavaApplication2 {

    public static void main(String [] args) {
        Scanner input = new Scanner(System.in);
        String name;
        double payRate;
        int hours;
        Boolean OTyn;
        int OTy;

        System.out.println("What is your name?");
        name = input.nextLine();
        System.out.println("What is your hourly rate of pay?");
        payRate = input.nextDouble();
        System.out.println("How any hours have you worked?");
        hours = input.nextInt();
        System.out.println("Your boss pays overtime. Answer with true or false.");
        OTyn = input.nextBoolean();
        if (OTyn == true) 
            System.out.println("After how many hours are you paid for overtime?");
            OTy = input.nextInt();
            if (hours > OTy)
                System.out.println("Your weekly pay is " + ((40 * payRate) + (hours - OTy)(payRate * 1.5)));
            else
                System.out.println("Your weekly pay is " + payRate * hours);
        else
            System.out.println("Your weekly pay is " + payRate * hours);
    }
}

3 个答案:

答案 0 :(得分:0)

我认为你错过了ifs上的{}

if (OTyn == true) { //here
    System.out.println("After how many hours are you paid for overtime?");
    OTy = input.nextInt();
    if (hours > OTy){ // Here doesn need because it is just one line after the if
        System.out.println("Your weekly pay is " + 
                 ((40*payRate) + (hours - OTy)(payRate*1.5))); 
     // (hours - OTy)(payRate*1.5) you are missing an operator between this expressions

    }else{
        System.out.println("Your weekly pay is " + payRate*hours);
    }
} else { //here
    System.out.println("Your weekly pay is " + payRate*hours);
} //and here

另外,对于只有一行的IF语句,您不需要{},因此:

if ( something )
   System.out.println("Something is true");
else
   System.out.println("Something is false");

答案 1 :(得分:0)

您错过了一组大括号和一个星号,如下所示。

if (OTyn == true) { // <-- This brace to open.
  System.out
      .println("After how many hours are you paid for overtime?");
  OTy = input.nextInt();
  if (hours > OTy)
    System.out.println("Your weekly pay is "
        + ((40 * payRate) + (hours - OTy)
            * (payRate * 1.5))); // <-- at the start of this line.
  else
    System.out.println("Your weekly pay is "
        + payRate * hours);
} else // <-- That brace to close.
  System.out.println("Your weekly pay is "
      + payRate * hours);

答案 2 :(得分:0)

import java.util.Scanner;

public class JavaApplication2 
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name;
double payRate;
int hours;
Boolean OTyn;
int OTy;

System.out.println("What is your name?");
name = input.nextLine();
System.out.println("What is your hourly rate of pay?");
payRate = input.nextDouble();
System.out.println("How any hours have you worked?");
hours = input.nextInt();
System.out.println("Your boss pays overtime. Answer with true or false.");
OTyn = input.nextBoolean();
if (OTyn == true){
    System.out.println("After how many hours are you paid for overtime?");
    OTy = input.nextInt();
    if (hours > OTy){
        System.out.println("Your weekly pay is " + ((40*payRate) + (hours - OTy)*(payRate*1.5)));
    }
    else{
        System.out.println("Your weekly pay is " + payRate*hours);
    }
}
else {
    System.out.println("Your weekly pay is " + payRate*hours);
}
}    
}

试试这个,不仅你错过了大括号,你还遇到了这条线的问题

System.out.println("Your weekly pay is " + ((40*payRate) + (hours - OTy)(payRate*1.5)));

您需要(hours - OTy)(payRate*1.5)之间的*才能生成(hours - OTy)*(payRate*1.5),否则字符串连接将无效