谢谢楼上,为啥子我这个代码会出错?import javax.swing.JOptionPane; public class Shuzu4 { public void Define() { String word=JOptionPane.showInputDialog("Enter the word:"); for(int i=0;i<c.length;i++) { if(word.equals(c[i])) { JOptionPane.showMessageDialog(null,"Yes"); } else { JOptionPane.showMessageDialog(null,"No"); } } } public static void main(String[] args) { char[] c={'big','cat','small','dog','fish','mouse','tall','little','chick','duck'}; String str=new String(c); Define(); System.exit(0); } }
java和C还是很有一点区别的import javax.swing.JOptionPane;public class Shuzu4 { static String[] c = { "big", "cat", "small", "dog", "fish", "mouse", "tall", "little", "chick", "duck" }; public static void Define(String word) { for (int i = 0; i < c.length; i++) { if (word.equals(c[i])) { JOptionPane.showMessageDialog(null, "Yes"); break; } else { JOptionPane.showMessageDialog(null, "No"); break; } } } public static void main(String[] args) { String word = JOptionPane.showInputDialog("Enter the word:"); Define(word); } }
1.将数组转成List,然后用contains方法 2.用 java.util.Arrays.binarySearch(Object[] a, Object key) 方法两种方法都不用循环,几行代码就搞定
2.用 java.util.Arrays.binarySearch(Object[] a, Object key) 方法 binarySearch需要先排序的吧。。
那个..好像只有big是yes的其他都是no阿,i被赋值为0就没变过再麻烦你一下,给全分
是的,需要用 java.util.Arrays.sort(Object[] a) 先排序也就是1.定义数组,乱序(升序更好) 2.java.util.Arrays.sort(Object[] a) 3.读输入 4.java.util.Arrays.binarySearch(Object[] a, Object key)取得存在否,并输出结果 5.读下一个(回到3)
惭愧惭愧。。 import javax.swing.JOptionPane;public class Shuzu4 { static String[] c = { "big", "cat", "small", "dog", "fish", "mouse", "tall", "little", "chick", "duck" }; public static void Define(String word) { for (int i = 0; i < c.length; i++) { if (word.equals(c[i])) { JOptionPane.showMessageDialog(null, "Yes"); return; } } JOptionPane.showMessageDialog(null, "No"); } public static void main(String[] args) { String word = JOptionPane.showInputDialog("Enter the word:"); Define(word); } }
这里给出一份简单实现,没有考虑效率: import javax.swing.JOptionPane;/** * * @author 红猎人 * @version 1.0 */ public class Main { static String[] c = {"big", "cat", "small", "dog", "fish", "mouse", "tall", "little", "chick", "duck"}; /** * 判断 word 是否在数组中 * @param word * @return */ public static boolean isDefine(String word) { for (int i = 0; i < c.length; i++) { if (word.equals(c[i])) { return true; } } // 不在 return false; } /** * @param args the command line arguments */ public static void main(String[] args) { for (;;) { String word = JOptionPane.showInputDialog("Enter the word:"); if (word == null || word.trim().equals("")) { // 点击取消 或 没有输入 退出程序 System.exit(0); } if (isDefine(word)) { JOptionPane.showMessageDialog(null, "Yes"); }else { JOptionPane.showMessageDialog(null, "No"); } } } }为了方便测试,我用了一个无限循环。希望能对你有帮助!
如果要从命令行读入参数,则代码应该这样: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;/** * * @author 红猎人 * @version 1.1 */ public class Main { static String[] c = {"big", "cat", "small", "dog", "fish", "mouse", "tall", "little", "chick", "duck"}; /** * 判断 word 是否在数组中 * @param word * @return */ public static boolean isDefine(String word) { for (int i = 0; i < c.length; i++) { if (word.equals(c[i])) { return true; } } // 不在 return false; } /** * @param args the command line arguments */ public static void main(String[] args) { String word; BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); try { for (;;) { // String word = JOptionPane.showInputDialog("Enter the word:"); System.out.print("Enter the word:"); // 从控制台读入数据 word = bf.readLine(); // 点击取消 或 没有输入 退出程序 if (word == null || word.trim().equals("")) { System.exit(0); } if (isDefine(word.trim())) { // 这里修补上一个版本的一个小漏洞 System.out.println("Yes"); } else { System.out.println("No"); } } } catch (IOException e) { // 输入出错 System.out.println("Error:" + e.getMessage()); } } }
1.1 版本还有一个Bug哈,应该是这样的: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;/** * * @author 红猎人 * @version 1.1 */ public class Main { static String[] c = {"big", "cat", "small", "dog", "fish", "mouse", "tall", "little", "chick", "duck"}; /** * 判断 word 是否在数组中 * @param word * @return */ public static boolean isDefine(String word) { for (int i = 0; i < c.length; i++) { if (word.equals(c[i])) { return true; } } // 不在 return false; } /** * @param args the command line arguments */ public static void main(String[] args) { String word; BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); try { for (;;) { // String word = JOptionPane.showInputDialog("Enter the word:"); System.out.print("Enter the word:"); // 从控制台读入数据 word = bf.readLine(); // 点击取消 或 没有输入 退出程序 if (word == null || word.trim().equals("")) { System.exit(0); } if (isDefine(word.trim())) { // 这里修补上一个版本的一个小漏洞 System.out.println("Yes"); } else { System.out.println("No"); } } } catch (IOException e) { // 输入出错 System.out.println("Error:" + e.getMessage()); } finally { try { bf.close(); } catch (IOException ex) { System.out.println("关闭流出错:" + ex.getMessage()); } } } }
public class Shuzu4
{
public void Define()
{
String word=JOptionPane.showInputDialog("Enter the word:");
for(int i=0;i<c.length;i++)
{ if(word.equals(c[i]))
{
JOptionPane.showMessageDialog(null,"Yes");
}
else
{
JOptionPane.showMessageDialog(null,"No");
}
}
}
public static void main(String[] args)
{
char[] c={'big','cat','small','dog','fish','mouse','tall','little','chick','duck'};
String str=new String(c);
Define();
System.exit(0);
}
}
static String[] c = { "big", "cat", "small", "dog", "fish", "mouse",
"tall", "little", "chick", "duck" }; public static void Define(String word) {
for (int i = 0; i < c.length; i++) { if (word.equals(c[i])) {
JOptionPane.showMessageDialog(null, "Yes");
break;
} else {
JOptionPane.showMessageDialog(null, "No");
break;
}
}
} public static void main(String[] args) {
String word = JOptionPane.showInputDialog("Enter the word:");
Define(word);
}
}
2.用 java.util.Arrays.binarySearch(Object[] a, Object key) 方法两种方法都不用循环,几行代码就搞定
binarySearch需要先排序的吧。。
java.util.Arrays.sort(Object[] a)
先排序也就是1.定义数组,乱序(升序更好)
2.java.util.Arrays.sort(Object[] a)
3.读输入
4.java.util.Arrays.binarySearch(Object[] a, Object key)取得存在否,并输出结果
5.读下一个(回到3)
import javax.swing.JOptionPane;public class Shuzu4 {
static String[] c = { "big", "cat", "small", "dog", "fish", "mouse",
"tall", "little", "chick", "duck" }; public static void Define(String word) {
for (int i = 0; i < c.length; i++) { if (word.equals(c[i])) {
JOptionPane.showMessageDialog(null, "Yes");
return;
}
}
JOptionPane.showMessageDialog(null, "No");
} public static void main(String[] args) {
String word = JOptionPane.showInputDialog("Enter the word:");
Define(word);
}
}
import javax.swing.JOptionPane;/**
*
* @author 红猎人
* @version 1.0
*/
public class Main { static String[] c = {"big", "cat", "small", "dog", "fish", "mouse",
"tall", "little", "chick", "duck"}; /**
* 判断 word 是否在数组中
* @param word
* @return
*/
public static boolean isDefine(String word) {
for (int i = 0; i < c.length; i++) {
if (word.equals(c[i])) {
return true;
}
}
// 不在
return false;
} /**
* @param args the command line arguments
*/
public static void main(String[] args) {
for (;;) {
String word = JOptionPane.showInputDialog("Enter the word:");
if (word == null || word.trim().equals("")) { // 点击取消 或 没有输入 退出程序
System.exit(0);
}
if (isDefine(word)) {
JOptionPane.showMessageDialog(null, "Yes");
}else {
JOptionPane.showMessageDialog(null, "No");
}
}
}
}为了方便测试,我用了一个无限循环。希望能对你有帮助!
如果要从命令行读入参数,则代码应该这样:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;/**
*
* @author 红猎人
* @version 1.1
*/
public class Main { static String[] c = {"big", "cat", "small", "dog", "fish", "mouse",
"tall", "little", "chick", "duck"}; /**
* 判断 word 是否在数组中
* @param word
* @return
*/
public static boolean isDefine(String word) {
for (int i = 0; i < c.length; i++) {
if (word.equals(c[i])) {
return true;
}
}
// 不在
return false;
} /**
* @param args the command line arguments
*/
public static void main(String[] args) {
String word;
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); try {
for (;;) {
// String word = JOptionPane.showInputDialog("Enter the word:");
System.out.print("Enter the word:");
// 从控制台读入数据
word = bf.readLine(); // 点击取消 或 没有输入 退出程序
if (word == null || word.trim().equals("")) {
System.exit(0);
} if (isDefine(word.trim())) { // 这里修补上一个版本的一个小漏洞
System.out.println("Yes");
} else {
System.out.println("No");
}
}
} catch (IOException e) {
// 输入出错
System.out.println("Error:" + e.getMessage());
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;/**
*
* @author 红猎人
* @version 1.1
*/
public class Main { static String[] c = {"big", "cat", "small", "dog", "fish", "mouse",
"tall", "little", "chick", "duck"}; /**
* 判断 word 是否在数组中
* @param word
* @return
*/
public static boolean isDefine(String word) {
for (int i = 0; i < c.length; i++) {
if (word.equals(c[i])) {
return true;
}
}
// 不在
return false;
} /**
* @param args the command line arguments
*/
public static void main(String[] args) {
String word;
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); try {
for (;;) {
// String word = JOptionPane.showInputDialog("Enter the word:");
System.out.print("Enter the word:");
// 从控制台读入数据
word = bf.readLine(); // 点击取消 或 没有输入 退出程序
if (word == null || word.trim().equals("")) {
System.exit(0);
} if (isDefine(word.trim())) { // 这里修补上一个版本的一个小漏洞
System.out.println("Yes");
} else {
System.out.println("No");
}
}
} catch (IOException e) {
// 输入出错
System.out.println("Error:" + e.getMessage());
} finally {
try {
bf.close();
} catch (IOException ex) {
System.out.println("关闭流出错:" + ex.getMessage());
}
}
}
}