tryarray.java
来自「Java 入门书的源码」· Java 代码 · 共 33 行
JAVA
33 行
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.
/* Searches an array for the first occurrence of a value
* input by the user. Uses a break statement to exit the
* loop if the value is found. Uses a flag to indicate that
* the value has been found.
*/
import iopack.Io;
public class TryArray {
public static void main(String [] args) {
int [] score = {56, 91, 22, 87, 49, 89, 65};
int testValue; // the value to search for
boolean found; // flag indicated value found
char repeat; // 'Y' to repeat 'N' to quit
do {
found = false;
testValue = Io.readInt("Enter a score");
for (int i=0; i < score.length; i++)
if (testValue == score[i]) {
System.out.println("Found score "+ testValue +" at index "+i);
found = true;
break;
}
if (!found) System.out.println("Score "+testValue+" not found");
repeat = Io.readChar("Enter 'Y' to repeat, 'N' to quit");
} while (repeat == 'Y' || repeat == 'y');
Io.readString("Press any key to exit"); // Added for IDE use
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?