2013-10-29 68 views
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语句

+3

是什么类型'yourTickets'并可以'''与该类型一起使用? –

+0

我可以看到的一个错误是你的if(yourTickets> minimumAmount)需要一个{在它之后 –

+0

对不起,我应该说'if'行有一个错误,说明>是未定义的参数类型。双倍字符串 –

回答

1

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

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

你不应该用double类型比较一个String类型。此外,对于字符串,您必须使用.compareTo().equals(),因为您可以使用>,<,>=,<===代替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

+1

我会通过他们的教程现在非常感谢你,你一直很好的帮助! –