如题所述
// Filename: country.java
// This program is used for outputing the countries according to the user input;
//
// Paper solution;
// 1. Catch the first letter from user input;
// 2. Transfer into switch statement;
// 3. Output all possible country names as string;
// 4. Display warning message to warn a wrong user input;
// 5. If wrong, continue the warning message until get a valid user input;
import java.io.*;
import java.io.IOException;
import java.util.*;
import javax.swing.JOptionPane;
import java.lang.*;
import java.lang.String;
public class country
{
public static void main (String[] args)
{
// 1. Catch the first letter from user input;
String topLetter = JOptionPane.showInputDialog(null, "Please input the first letter of the country", "TopLetter", JOptionPane.QUESTION_MESSAGE);
char a = topLetter.charAt(0);
// 2. Transfer into switch statement;
switch (a)
{
// 3. Output all possible country names as string;
case 'E': JOptionPane.showMessageDialog(null,"England","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'F': JOptionPane.showMessageDialog(null,"France","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'S': JOptionPane.showMessageDialog(null,"Switzerland, Slovakia, Slovenia, Spain, Serbia","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'P': JOptionPane.showMessageDialog(null,"Portugal","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'H': JOptionPane.showMessageDialog(null,"Holland","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'I': JOptionPane.showMessageDialog(null,"Italy","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'G': JOptionPane.showMessageDialog(null,"Germany","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'D': JOptionPane.showMessageDialog(null,"Denmark","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
/*
case 'F': System.out.println("France"); break;
case 'S': System.out.println("Switzerland, Slovakia, Slovenia, Spain, Serbia"); break;
case 'P': System.out.println("Portugal"); break;
case 'H': System.out.println("Holland"); break;
case 'I': System.out.println("Italy"); break;
case 'G': System.out.println("Germany, Greece"); break;
case 'D': System.out.println("Denmark"); break;
*/
// 4. Display warning message to warn a wrong user input;
default: JOptionPane.showMessageDialog(null,"Incorrect input, please input the right country name","Warning Message",JOptionPane.INFORMATION_MESSAGE);
//default: System.out.println("Incorrect input, please input the right country name");
}
}
}
// This program is used for outputing the countries according to the user input;
//
// Paper solution;
// 1. Catch the first letter from user input;
// 2. Transfer into switch statement;
// 3. Output all possible country names as string;
// 4. Display warning message to warn a wrong user input;
// 5. If wrong, continue the warning message until get a valid user input;
import java.io.*;
import java.io.IOException;
import java.util.*;
import javax.swing.JOptionPane;
import java.lang.*;
import java.lang.String;
public class country
{
public static void main (String[] args)
{
// 1. Catch the first letter from user input;
String topLetter = JOptionPane.showInputDialog(null, "Please input the first letter of the country", "TopLetter", JOptionPane.QUESTION_MESSAGE);
char a = topLetter.charAt(0);
// 2. Transfer into switch statement;
switch (a)
{
// 3. Output all possible country names as string;
case 'E': JOptionPane.showMessageDialog(null,"England","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'F': JOptionPane.showMessageDialog(null,"France","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'S': JOptionPane.showMessageDialog(null,"Switzerland, Slovakia, Slovenia, Spain, Serbia","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'P': JOptionPane.showMessageDialog(null,"Portugal","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'H': JOptionPane.showMessageDialog(null,"Holland","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'I': JOptionPane.showMessageDialog(null,"Italy","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'G': JOptionPane.showMessageDialog(null,"Germany","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
case 'D': JOptionPane.showMessageDialog(null,"Denmark","Country Name",JOptionPane.INFORMATION_MESSAGE);
break;
/*
case 'F': System.out.println("France"); break;
case 'S': System.out.println("Switzerland, Slovakia, Slovenia, Spain, Serbia"); break;
case 'P': System.out.println("Portugal"); break;
case 'H': System.out.println("Holland"); break;
case 'I': System.out.println("Italy"); break;
case 'G': System.out.println("Germany, Greece"); break;
case 'D': System.out.println("Denmark"); break;
*/
// 4. Display warning message to warn a wrong user input;
default: JOptionPane.showMessageDialog(null,"Incorrect input, please input the right country name","Warning Message",JOptionPane.INFORMATION_MESSAGE);
//default: System.out.println("Incorrect input, please input the right country name");
}
}
}
温馨提示:答案为网友推荐,仅供参考