}
}这样,如果你不输入name.age,就无法创建类的对象,因为系统给的空构造函数已经不在了
2)老师课堂上讲的猜字游戏并没有容错机制,但是输入100以上的数字也是没问题的,加一个容错机制,让程序更加温馨,代码如下:
import java.util.Scanner;
class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int guessNum= (int)(Math.random()*100)+1;
System.out.println("请输入一个1到100之间的数字");
while (true) {
int input = sc.nextInt();
if (input <1 || input >100){
System.out.println("请您输入正确的数字,1到100之间");
continue;
}
else if (input > guessNum){
System.out.println("您猜大了");
}else if (input < guessNum){
System.out.println("您猜的小了");
} else{
System.out.println("恭喜您过关");
break;
}
}
}
}
3)选择排序的另外一种方法,用一个临时变量记录住那个数,最后再换位置:
class Demo {
public static void main(String[] args) {
int [] arr = {3,7,8,4,9,3};
printArray(arr);
selectSort(arr);
printArray(arr);
}
public static void printArray(int [] arr) {
System.out.print("[");
for (int x = 0;x< arr.length ;x++ ) {
if (x == arr.length-1){
System.out.print(arr[x]);
}else{
System.out.print(arr[x]+",");
}
}
System.out.println("]");
}
public static void selectSort(int []arr){
for (int x =0;x <arr.length-1 ;x++ ) {
int num = arr[x];
int index = x;
for (int y = x+1;y <arr.length ;y++ ) {
if (num > arr[y]) {
num = arr[y];
index = y;
}
}
if (index!= x){
swap(arr,index,x);
}
}
}
public static void swap(int [] arr,int a,int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
4)导包,超简单的排序:
import java.util.Arrays;
class Demo {
public static void main(String[] args) {
int [] arr = {3,7,8,4,9};
printArray(arr);
Arrays.sort(arr);
printArray(arr);
}
public static void printArray(int [] arr) {
System.out.print("[");
for (int x = 0;x< arr.length ;x++ ) {
if (x == arr.length-1){
System.out.print(arr[x]);
}else{
System.out.print(arr[x]+",");
}
}
System.out.println("]");
}
public static void print99(int a) {
for (int x = 1; x <= a; x++) {
for (int y = 1; y <= x; y++) {
System.out.print(y + "*" + x + "=" + y * x + "\t");
}
System.out.println();
}
}
}