所以我有这个项目用于我的计算机课程,无论我尝试多少种不同的方式,我似乎都无法运行我的程序。我想要做的是让程序检查用户输入的内容是否等于三个单词中的任何一个(Cookies,Milk,Both),如果它不再问这个问题并使用该输入但是因为我是新的到java我似乎无法让它工作
这是我的代码:
import javax.swing.*;
import java.util.*;
public class Cookie {
public static void main(String[] args) {
try {
Info info = new Info();
} catch(Exception e) {
System.err.println("Error! " + e.getMessage());
}
}
static class Info {
String inputs = JOptionPane.showInputDialog(null,
"What do you want, Cookies, Milk or Both?");
String word1 = "Cookies";
String word2 = "milk";
String word3 = "Both";
String flagger = "";
while (true)
if(inputs.length() !=0) {
}
for(int i=0; i<inputs.length(); i++)
{
for(int j=0; j<word1.length(); j++)
{
if(inputs.charAt(i)==word1.charAt(j))
{
flagger=flagger+word1.charAt(i)+"";
}
}
for(int j=0; j<word2.length(); j++)
{
if(inputs.charAt(i)==word2.charAt(j))
{
flagger=flagger+word2.charAt(i)+"";
}
}
for(int j=0; j<word3.length(); j++)
{
if(inputs.charAt(i)==word3.charAt(j))
{
flagger=flagger+word3.charAt(i)+"";
}
}
if(!(inputs.equalsIgnoreCase(flagger))) {
String message = String.format("%s", "Huh, I didn't get that, please say it again");
JOptionPane.showMessageDialog(null, message);
String inputs = JOptionPane.showInputDialog(null,
"What do you want, Cookies, Milk or Both?");
}
if(inputs.equalsIgnoreCase("cookies")) {
String message = String.format("%s", "Here have some Cookies :)");
JOptionPane.showMessageDialog(null, message);
}
if(inputs.equalsIgnoreCase("MILK")) {
String message = String.format("%s", "Here is the Milk you wanted :)");
JOptionPane.showMessageDialog(null, message);
}
if(inputs.equalsIgnoreCase("BOTH")) {
String message = String.format("%s", "Here is your Cookies and Milk :)");
JOptionPane.showMessageDialog(null, message);
}
}
}
}
}
}
答案 0 :(得分:3)
我做了一些修复后能够编译你的代码。我使用了一种不同的方法,这种方法很有效,至少在大多数情况下都是如此。
这是我的Cookie.java
public class Cookie {
public static void main(String[] args) {
try {
Info info = new Info();
info.checkInput();
} catch(Exception e) {
System.err.println("Error! " + e.getMessage());
}
}
}
和Info.java
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
public class Info {
public void checkInput() {
String inputs = JOptionPane.showInputDialog(null,
"What do you want, Cookies, Milk or Both?");
Map<String, String> words = new HashMap<String, String>();
words.put("cookies", "Here have some Cookies :)");
words.put("milk", "Here is the Milk you wanted :)");
words.put("both", "Here is your Cookies and Milk :)");
while (true) {
if (inputs != null && inputs.length() > 0) {
if (words.containsKey(inputs.toLowerCase())) {
JOptionPane.showMessageDialog(null,
words.get(inputs.toLowerCase()));
inputs = repeat();
} else {
JOptionPane.showMessageDialog(null,
"Huh, I didn't get that, please say it again");
inputs = repeat();
}
} else {
inputs = repeat(); }
}
}
private String repeat() {
return JOptionPane.showInputDialog(null,
"What do you want, Cookies, Milk or Both?");
}
}
答案 1 :(得分:2)
您的代码无效,因为:
static class Info
上的static void Info(){
,并仅使用Info()
(Info info = new Info();
main
方法中的String inputs = JOptionPane.showInputDialog(null,"What do you want, Cookies, Milk or Both?");
进行调用。inputs = JOptionPane.showInputDialog(null,"What do you want, Cookies, Milk or Both?");
,只需第二次使用while (true)
if(inputs.length() !=0) {
} // this is problematic bracket
即可。你混合了一些括号,如:
import javax.swing.*;
public class Cookie {
public static void main(String[] args) {
while (true){
String inputs = JOptionPane.showInputDialog(null,
"What do you want, Cookies, Milk or Both?");
if(inputs.equalsIgnoreCase("cookies")) {
JOptionPane.showMessageDialog(null, "Here have some Cookies :)");
break;
}else if(inputs.equalsIgnoreCase("MILK")) {
JOptionPane.showMessageDialog(null, "Here is the Milk you wanted :)");
break;
}else if(inputs.equalsIgnoreCase("BOTH")) {
JOptionPane.showMessageDialog(null, "Here is your Cookies and Milk :)");
break;
}else{
JOptionPane.showMessageDialog(null, "Huh, I didn't get that, please say it again");
}
}
}
}
所以它根本不起作用,并且所有嵌套的语句都与它有关。
您需要修复它以至少运行您的应用程序。
修改强>
您似乎有很多不必要的代码,您可以将其缩短为例如:
{% if page.url == "/pages/about" %}
<div class="thebnr"><img src="{{ 'thebnr.png' | asset_url }}" alt="{{ the.name }}" itemprop="bnr"></div>
{% else %}
other thing
{% endif %}
答案 2 :(得分:1)
您似乎希望从用户那里获得输入,并将其与某些预设字符串(Cookie,牛奶和两者)进行比较
如果你把整个事情放到一个while(true)
循环中,并且在从用户那里获得输入之后你写了类似
String message;
Boolean isValid = true;
if (inputs.equalsIgnoreCase("Cookies")){
message = "Have some cookies";
}
...
else{
message = "Try again";
isValid = false;
}
JOptionPane.showMessageDialog(null, message);
if(isValid) break;
注意:我在手机上写这个,所以语法可能不准确。请自行决定。