Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

What's wrong with this program


  • Please log in to reply

#1
skrtel_williams

skrtel_williams

    New Member

  • Member
  • Pip
  • 1 posts
Hi everyone
for this program


import javax.swing.JOptionPane;
public class TEMPO
{
public static void main (String args[])
{
int temp;
String tempString;
String contString;
char charAns;

do
{

tempString = JOptionPane.showInputDialog(null, "Enter the temperature:",
"Input",JOptionPane.QUESTION_MESSAGE);
temp = Integer.parseInt(tempString);

if (temp < 10 )
JOptionPane.showMessageDialog(null,"Too Cold", "Output",
JOptionPane.INFORMATION_MESSAGE);

else if (temp >= 10 && temp <= 20)
JOptionPane.showMessageDialog(null,"Average", "Output",
JOptionPane.INFORMATION_MESSAGE);

else if (temp >= 21 && temp <= 30)
JOptionPane.showMessageDialog(null, "Hot", "Output",
JOptionPane.INFORMATION_MESSAGE);

else
JOptionPane.showMessageDialog(null, "Too Hot", "Output",
JOptionPane.INFORMATION_MESSAGE);

contString =
JOptionPane.showInputDialog(null, "Would you like to continue (y/n):",
"Input",JOptionPane.QUESTION_MESSAGE);

charAns = contString.charAt(0);

}while (charAns !='n');

System.exit(0);
}
}

the problem is, when i enter other that 'y', the program accepts it.how to make it only accept 'y' as the answer.

pls help...
tq & rgds
  • 0

Advertisements


#2
coyne20

coyne20

    Member

  • Member
  • PipPip
  • 35 posts
I dont think your no option works whatsoever. Try the following below:

import javax.swing.JOptionPane;
import javax.swing.*;

public class TEMPO
{
//private static final JOptionPane = null;

public static void main (String args[])
{
int temp;
String tempString;
int contString;
char charAns;

int Names;
do
{

tempString = JOptionPane.showInputDialog(null, "Enter the temperature:",
"Input",JOptionPane.QUESTION_MESSAGE);
temp = Integer.parseInt(tempString);

if (temp < 10 )
JOptionPane.showMessageDialog(null,"Too Cold", "Output",
JOptionPane.INFORMATION_MESSAGE);

else if (temp >= 10 && temp <= 20)
JOptionPane.showMessageDialog(null,"Average", "Output",
JOptionPane.INFORMATION_MESSAGE);

else if (temp >= 21 && temp <= 30)
JOptionPane.showMessageDialog(null, "Hot", "Output",
JOptionPane.INFORMATION_MESSAGE);

else
JOptionPane.showMessageDialog(null, "Too Hot", "Output",
JOptionPane.INFORMATION_MESSAGE);

Names = JOptionPane.showOptionDialog(null,
"Would you like to continue?",
"Confirmation",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,null,null,null);

}while(Names != 1);

if(Names == 1)
{
System.exit(0);
}

}

}
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP