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

时间:2015-05-30 22:06:27

标签: java

修改:我的问题已得到部分解决。现在我可以输入文本,但输入'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");
        }


    }

}

3 个答案:

答案 0 :(得分:6)

您的代码中有2个问题:
1.使用input.next()代替input.nextLine()
2。将您的if conditional更改为ship.equals("shiphalf")。请记住,您使用.equals()来比较2个字符串。

这是您的最终代码:

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

答案 1 :(得分:2)

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

double cost = input.nextDouble();

您的扫描仪会在控制台中读取下一个Double,然后&#34;停止&#34;在那个双重值的末尾。它不会进入新线。 查看Java Documentation,我们可以看到方法nextLine()部分地通过以下方式描述:

  

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

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

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

答案 2 :(得分:1)

从用户获取文本的另一种方法是使用JOptionPane。另外,如果你要比较文字,你想要编码&#39; ship.equals(shiphalf)&#39;

    String x = JOptionPane.showInputDialog(null, "Type something!");
    if(ship.equals(x)){
            System.out.println("Your code works"); 
    }

希望有这种帮助?