第十周Java作业
作业
1.
package zuoye;
public class zuoye1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int x[] = { 4, 3, 8, 7, 6, 4 };
paiXu(x);
for (int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
}
public static void paiXu(int a[]) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
}
2.
package zuoye;
public class zuoye2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum1=jc(10);
System.out.println(sum1);
}
public static int jc(int n){
int sum=1;
for (int i =1; i <=n; i++) {
sum*=i;
}
return sum;
}
}
3.
package zuoye;
import java.util.Scanner;
public class zuoye3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub year();
}
public static void year(){
Scanner d=new Scanner(System.in);
System.out.println("输入年份");
int year=d.nextInt();
if(year%4==0&&year%100!=0||year%400==0){
System.out.println(year+"是闰年");
}else{
System.out.println(year+"是平年");
}
}
}
4.
package dibazhou;
import java.util.Random;
import java.util.Scanner;
public class zuoye4 {
/**
* @param args
*/
public static void mainMenu() {
Scanner input = new Scanner(System.in);
System.out.println("欢迎使用本系统");
System.out.println("1.登录");
System.out.println("2.注册");
System.out.println("3.幸运抽奖");
System.out.println("4.退出");
System.out.println("请选择");
int i = input.nextInt();
switch (i) {
case 1:
login();
break;
case 2:
reg();
break;
case 3:
lucky();
}
}
public static void lucky() {
Scanner input = new Scanner(System.in);
System.out.println("输入一个四位数");
Random r = new Random();
int x = r.nextInt(10);
int num = input.nextInt();
while (num < 1000 || num > 10000) {
System.out.println("输入有误,重新输入");
num = input.nextInt();
if (num >= 1000 && num < 10000) {
break;
}
}
int bai = num % 1000 / 100;
if (bai == x) {
System.out.println("幸运会员");
} else {
System.out.println("不是幸运会员");
}
returnMain();
}
public static void returnMain() {
Scanner input = new Scanner(System.in);
System.out.println("是否返回主菜单?");
if (input.next().equalsIgnoreCase("Y"))
mainMenu();
else
System.out.println("谢谢使用");
}
public static void reg() {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("输入要注册的用户名");
String uname = input.next();
System.out.println("输入注册密码");
String upwd = input.next();
System.out.println("注册成功");
returnMain();
}
public static void login() {
Scanner input = new Scanner(System.in);
System.out.println("输入用户名");
String uname = input.next();
System.out.println("输入密码");
String upwd = input.next();
if (uname.equals("zs") && upwd.equals("123")) {
System.out.println("ok");
} else {
System.out.println("fail");
}
returnMain();
}
public static void main(String[] args) {
mainMenu();
}
}