为什么我的java程序不接受用户的字母输入?

为什么我的java程序不接受用户的字母输入?,java,Java,编辑:我的问题已部分修复。现在我可以输入文本,但是输入'shiphalf'值后,没有任何内容可以读取。我是否错误地构造了if-else语句 我试图让人们输入优惠券代码,但我无法弄清楚如何让控制台知道用户正在输入文本。我觉得错误就在这里: System.out.println("Enter a Coupon Code: "); String ship = input.nextLine(); 但我不能完全肯定。下面是完整的源代码: package pseudoP

编辑:我的问题已部分修复。现在我可以输入文本,但是输入'shiphalf'值后,没有任何内容可以读取。我是否错误地构造了if-else语句


我试图让人们输入优惠券代码,但我无法弄清楚如何让控制台知道用户正在输入文本。我觉得错误就在这里:

        System.out.println("Enter a Coupon Code: ");
        String ship = input.nextLine(); 
但我不能完全肯定。下面是完整的源代码:

package pseudoPackage;

import java.util.Scanner;

public class PseudoCode {

    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);


        int ship1 = 0;
        int ship2 = 6;
        int ship3 = 2;
        String shiphalf = null;

        System.out.print("How much will shipping cost you?");
        System.out.println();
        System.out.print("Enter your textbook cost in dollars without the $: ");
        double cost = input.nextDouble();



        if ( cost >= 100 ) {
            System.out.println("Your shipping costs are $0");
        } else if ( cost < 100 && cost > 50 ) {
            System.out.println("Your shipping cost is $6");
        } else if ( cost < 50 ) {
            System.out.println("Your shipping cost is $2");
        }

        System.out.println("Enter a Coupon Code: ");
        String ship = input.nextLine(); 

        if ( ship == shiphalf && cost >= 100 ) {
            System.out.println("Your shipping costs are $0");
        } else if ( ship == shiphalf && cost < 100 && cost >50 ) {
            System.out.println("Your shipping costs are $3 ");
        } else if ( ship == shiphalf && cost < 50 ) {
            System.out.println("Your shipping costs are $1");
        }


    }

}
package伪包;
导入java.util.Scanner;
公共类伪码{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int ship1=0;
int-ship2=6;
int-ship3=2;
字符串shiphalf=null;
System.out.print(“运费是多少?”);
System.out.println();
System.out.print(“以美元为单位输入教科书成本,不带美元:”;
双倍成本=input.nextDouble();
如果(成本>=100){
System.out.println(“您的运费为$0”);
}否则,如果(成本<100&成本>50){
System.out.println(“您的运费是6美元”);
}否则,如果(成本<50){
System.out.println(“您的运费是2美元”);
}
System.out.println(“输入优惠券代码:”);
字符串ship=input.nextLine();
如果(发货==发货一半和成本>=100){
System.out.println(“您的运费为$0”);
}否则,如果(发货==发货一半和成本<100和成本>50){
System.out.println(“您的运费是3美元”);
}否则,如果(发货==发货一半和成本<50){
System.out.println(“您的运费为1美元”);
}
}
}

您的代码中有两个问题:
1.使用
input.next()
而不是
input.nextLine()

二,。将您的
if conditional
更改为
ship.equals(“shiphalf”)
。请记住,您使用
.equals()
来比较两个字符串

这是您的最终代码:

package pseudoPackage;
import java.util.Scanner;

public class PseudoCode {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int ship1 = 0;
        int ship2 = 6;
        int ship3 = 2;
        String shiphalf = null;

        System.out.print("How much will shipping cost you?");
        System.out.print("\nEnter your textbook cost in dollars without the $: ");
        double cost = input.nextDouble();

        if ( cost >= 100 ) {
            System.out.println("Your shipping costs are $0");
        } else if ( cost < 100 && cost > 50 ) {
            System.out.println("Your shipping cost is $6");
        } else if ( cost < 50 ) {
            System.out.println("Your shipping cost is $2");
        }

        System.out.println("Enter a Coupon Code: ");
        String ship = input.next(); 

        if ( ship.equals("shiphalf") && cost >= 100 ) {
            System.out.println("Your shipping costs are $0");
        } else if ( ship.equals("shiphalf") && cost < 100 && cost >50 ) {
            System.out.println("Your shipping costs are $3 ");
        } else if ( ship.equals("shiphalf") && cost < 50 ) {
            System.out.println("Your shipping costs are $1");
        }
    }
}
package伪包;
导入java.util.Scanner;
公共类伪码{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int ship1=0;
int-ship2=6;
int-ship3=2;
字符串shiphalf=null;
System.out.print(“运费是多少?”);
System.out.print(“\n以美元输入教科书成本,不带美元:”;
双倍成本=input.nextDouble();
如果(成本>=100){
System.out.println(“您的运费为$0”);
}否则,如果(成本<100&成本>50){
System.out.println(“您的运费是6美元”);
}否则,如果(成本<50){
System.out.println(“您的运费是2美元”);
}
System.out.println(“输入优惠券代码:”);
字符串ship=input.next();
如果(装运等于(“一半装运”)&成本>=100){
System.out.println(“您的运费为$0”);
}否则,如果(装运等于(“一半装运”)&成本<100&成本>50){
System.out.println(“您的运费是3美元”);
}否则,如果(装运等于(“一半装运”)&成本<50){
System.out.println(“您的运费为1美元”);
}
}
}

从用户处获取文本的另一种方法是使用JOptionPane。此外,如果您正在比较文本,则需要编写“ship.equals(shiphalf)”代码


希望这种帮助?

这里发生的事情如下: 当你打电话的时候

double cost = input.nextDouble();
扫描仪在控制台中读取下一个双精度值,然后在该双精度值的末尾“停止”。它不输入新行。 查看,我们可以看到方法nextLine()的部分描述方式如下:

使此扫描仪前进超过当前行并返回跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符

因此,如果您的扫描仪仍在一行中,并且您调用nextLine(),它将只返回该行的其余部分。在您的情况下,这很可能只是一个空字符串,即使您希望读取优惠券代码。 之后,扫描仪指向控制台中您想要读取的实际行


那么,解决这个问题最简单的方法是什么?在获取优惠券代码的输入之前,只需再次调用nextLine()方法。这会将扫描仪设置为要读取的实际行的开头

不要通过
==
比较字符串。此运算符比较对象引用,而不是字符串值。使用String类的
equals()
方法。例如'if(ship.equals(shiphalf)){do somethine here}'See的可能重复项您应该注意
问题。如果您输入
50
您将不会得到任何折扣。非常感谢!那太好了!!我刚刚开始学习Java,所以有点困惑,但感谢您提供了深入的答案@NicolasDry没问题!谢谢你提供的细节!另外,在比较字符串与.equals()和not==时,这可能会很有用。请记住,字符串是对象,而不是像int或double这样的基本数据类型。这使得他们的行为有点不同
double cost = input.nextDouble();