繁体   English   中英

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

[英]How do I combine strings and integers in a print statement?

我刚刚开始在大学课程中学习Java。 如果还没有涵盖所有陈述,但我认为我可以自己尝试。 我最后遇到了一些我认为可以解决的语法错误,但是现在当我尝试添加带有整数的字符串时似乎出现了操作数错误。

我班上的问题是向用户询问他们的姓名,小时工资率以及他们工作多少小时。 我知道教授很快就会要求我们向用户询问他们是否获得加班费。 如果他们这样做,请问加班了几个小时。 然后计算总付款额,其中任何加班费均为正常时薪的1.5倍。

但是我在最后3个打印语句中遇到错误,我认为这是因为我试图将字符串和整数组合在一起。 我该如何解决?

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);
    }
}

我认为您在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");

您缺少一组括号和一个星号,如下所示。

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);
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)之间(payRate*1.5)(hours - OTy)*(payRate*1.5)否则字符串连接将不起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM