0

因此,我正在制作一个程序,该程序基本上可以计算出为活动/活动售出的门票。我目前有一个链接到我的代码的外部文本文件,代码采用数字(这是某人应该为某个活动出售的门票数量),然后我希望用户使用对话框输入如何他们卖了很多票。使用 if 语句然后我希望输出是......“干得好,你已经卖出了足够多的票”或“你真的应该卖出更多票”之类的。这是我到目前为止所拥有的......

import java.util.*;
import java.io.*; 
import javax.swing.JOptionPane;

public class ticketjava
{ 
    public static void main(String[] args) throws FileNotFoundException 
    {

        Scanner inFile = new Scanner(new FileReader("C:\\TicketGoals.txt"));

        double minimumAmount; 
        double goodAmount;

            minimumAmount = inFile.nextDouble();
            goodAmount = inFile.nextDouble();

        String yourTickets;

        yourTickets = JOptionPane.showInputDialog("Enter your tickets sold:");
        if (yourTickets > minimumAmount)  

        JOptionPane.showInputDialog(null, "Well done you have sold enough tickets",  JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);

        inFile.close();

        }

    }   

正如您所看到的,我的 if 语句远未达到应有的位置,因为我真的很努力如何订购它,任何帮助将不胜感激,谢谢!我真的很挣扎我的 if 语句

4

1 回答 1

1

我相信您想将变量转换yourTickets为双精度,以便将其与变量进行比较minimumAmount。您可以使用该Double.parseDouble()方法。我建议阅读有关比较 Java 对象和数据类型的内容:

http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

您不应该将String类型与double类型进行比较。此外,您必须使用.compareTo()or.equals()用于字符串,而您可以使用>, <, >=, <=, 和==for double

使用 if 语句然后我希望输出是......“干得好,你已经卖出了足够多的票”或“你真的应该卖出更多票”之类的。这是我到目前为止所拥有的......

为此,您需要一个 if/else 语句。

import java.util.*;
import java.io.*; 
import javax.swing.JOptionPane;

public class ticketjava
{ 
    public static void main(String[] args) throws FileNotFoundException 
    {

        Scanner inFile = new Scanner(new FileReader("C:\\TicketGoals.txt"));

        double minimumAmount; 
        double goodAmount;

        minimumAmount = inFile.nextDouble();
        goodAmount = inFile.nextDouble();

        String yourTickets;

        yourTickets = JOptionPane.showInputDialog("Enter your tickets sold:");

        //you need to convert the String to a double
        //this will make it comparable with ">" in the below if statement
        double converted_yourTickets = Double.parseDouble(yourTickets);

        //added if/else
        //if condition A is true then do the follow...else do something different
        if (converted_yourTickets > minimumAmount){
            JOptionPane.showInputDialog(null, "Well done you have sold enough tickets",  JOptionPane.INFORMATION_MESSAGE);
        }
        else{
            JOptionPane.showInputDialog(null, "You should really of sold more tickets",  JOptionPane.INFORMATION_MESSAGE);
        }

        //close the file before doing system.exit(0)
        inFile.close();
        //but im not sure why you have it in the first place...
            //System.exit(0);

    }

}   

您似乎对 Java 很陌生,我建议您阅读以下 if/else:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

和数据类型:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

于 2013-10-29T17:20:59.950 回答