java训练

java基础练习题

 

新手训练,望大家多多指教

 

 

练习1

代码

public class demo01 {
   public static void main(String[] args) {
       String[] strings = new String[]{"Nike背包","Adidas运动衫","李宁运动鞋","Kappa外套","361°腰包"};
       for (int i = 0; i < strings.length; i++) {
           System.out.println("第"+i+"号索引元素为"+strings[i]);
      }
  }
}



 

练习2

代码

package lianxi;

import java.util.Scanner;

public class demo02 {
   public static void main(String[] args) {
       double moneys[]=new double[6];
       double moneySum=0;//总钱数
       Scanner sc=new Scanner(System.in);
       System.out.println("请输入会员本月的消费记录");
       for (int i = 1; i < 6; i++) {
           System.out.println("请输入第"+i+"笔购物金额");
           double money = sc.nextDouble();
           moneys[i-1] = money;
            moneySum = money+moneySum;//总钱数
      }
       //总额放进去
       moneys[5] = moneySum;
       System.out.println();
       System.out.println("序号           金额(元)");
       for (int i = 0; i < moneys.length; i++) {
           if (i == 5 ){
               System.out.println("总金额         "+moneys[5]);
               continue;
          }
           System.out.println(i+1+"             "+moneys[i]);

      }

  }
}



 

练习3

代码


package lianxi;

import java.util.Arrays;
import java.util.Scanner;

public class demo03 {
   public static void main(String[] args) {
       int[] sorces = new int[5];
       Scanner scanner = new Scanner(System.in);
       for (int i = 0; i < 5; i++) {
           System.out.println("请输入"+(i+1)+"号的成绩");
           int sorce = scanner.nextInt();
           sorces[i] = sorce;//赋值
      }
       Arrays.sort(sorces);
       System.out.print("排序后为");
       for (int i = 0; i < sorces.length; i++) {
           if (i == sorces.length-1){
               System.out.println(sorces[i]);
               continue;
          }
           System.out.print(sorces[i]+",");
      }
  }
}






 

练习4

代码

package lianxi;

import java.nio.charset.Charset;
import java.util.Arrays;

public class demo04 {

   public static void main(String[] args) {
       char[] chars = new char[]{'a', 'c', 'u', 'b', 'e', 'p', 'f', 'z'};
       System.out.print("原字符序列:");
       for (int i = 0; i < chars.length; i++) {
           System.out.print(chars[i] + "");
      }
       System.out.println();
       Arrays.sort(chars);
       System.out.print("升序排序后:");
       for (int i = 0; i < chars.length; i++) {
           System.out.print(chars[i] + "");
      }
       System.out.println();

       System.out.print("降序排序后:");
       //逆序 创建一个新数组,遍历填充
     /* char[] chars1 = new char[chars.length];
       for (int i = 0, j = chars1.length - 1; i < chars.length; i++, j--) {
           chars1[j] = chars[i];
       }

       for (int i = 0; i < chars1.length; i++) {
           System.out.print(chars1[i] + "");
       }
       */

       //冒泡排序
       char temp;
       for (int i = 1; i < chars.length; i++) {//需要排序几趟
           for (int j = 0;  j< chars.length-i; j++) {//这一趟需要排序几次
               if(chars[j]<chars[j+1]){
                   temp= chars[j];
                   chars[j] = chars[j+1];
                   chars[j+1] = temp;
              }

          }

      }

       for (int i = 0; i < chars.length; i++) {
           System.out.print(chars[i]+"");
      }

       //一半颠倒顺序
      /* char temp;
       for (int i = 0, j = chars.length - 1; i < chars.length / 2; i++, j--) {
           temp = chars[j];
           chars[j] = chars[i];
           chars[i] = temp;
       }
       for (int i = 0; i < chars.length; i++) {
           System.out.print(chars[i]+"");
       }*/

  }
}





 

练习5

代码

package lianxi;

import java.util.Scanner;

public class demo05 {
   public static void main(String[] args) {
       Double[] prices = new Double[4];
       Scanner scanner = new Scanner(System.in);
       System.out.println("请输入4家店的价格");
       for (int i = 0; i < 4; i++) {
           System.out.print("请输入第" + (i + 1) + "家的价格:");
           double price = scanner.nextDouble();
           prices[i] = price;
      }


       double priceMin = prices[0];
       for (int i = 0; i < prices.length; i++) {
           if (priceMin > prices[i]) {
               priceMin = prices[i];
          }
      }

       System.out.println("最低价格为"+priceMin);


       //冒泡排序
     /* double temp;
       for (int i = 0; i < prices.length - 1; i++) {//走几趟
           for (int j = 0; j < prices.length - i - 1; j++) {//每趟比几次
               if (prices[j] > prices[j + 1]) {
                   temp = prices[j];
                   prices[j] = prices[j + 1];
                   prices[j + 1] = temp;
               }
           }
       }


       for (int i = 0; i < prices.length; i++) {
           System.out.println(prices[i]);
       }
       System.out.println("最低价格是"+prices[0]);*/

  }
}





 

练习6

代码

package lianxi;

import java.util.Scanner;

public class demo06 {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       while (true) {
           System.out.print("请输入姓名:");
           String name = scanner.next();
           if (name.equals("n")) {
               System.out.println("退出程序");
               break;
          }
           System.out.print("请输入年龄:");
           int age = scanner.nextInt();
           String string = age <= 12 ? "免费" : "门票为20元";

           System.out.println(name + "的年龄为:" + age + ",门票为" + string);
           System.out.println();
           //break;
      }
  }
}





 

练习7

代码

package lianxi;

import java.util.Scanner;

public class demo07 {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.println("请输入卡类型");
       String cardType = scanner.next();
       System.out.println("请输入卡的积分");
       int integration = scanner.nextInt();
       Customer customer = new Customer(cardType, integration);
       test(customer);
  }

   public static void test(Customer customer) {
       //无此卡类型
       if (!"金卡".equals(customer.getCardType()) && !"普通卡".equals(customer.getCardType())) {
           System.out.println("输入错误,无此卡类型");
           return;
      }
       //有此卡,则可以输出并且进行判断
       customer.show();
       //判断是否是金卡
       if ("金卡".equals(customer.getCardType())) {
           //是金卡,判断积分
           if (customer.getIntegration() > 1000) {
               System.out.println("回馈积分500分");
          }
           return;
      } else if ("普通卡".equals(customer.getCardType())) {
           //是普通卡,判断积分
           if (customer.getIntegration() > 5000) {
               System.out.println("回馈积分500分");
          }
           return;
      }


  }


   //客户类
   public static class Customer {
       String cardType;
       int integration;

       public Customer() {
      }

       public Customer(String cardType, int integration) {
           this.cardType = cardType;
           this.integration = integration;
      }

       public String getCardType() {
           return cardType;
      }

       public void setCardType(String cardType) {
           this.cardType = cardType;
      }

       public int getIntegration() {
           return integration;
      }

       public void setIntegration(int integration) {
           this.integration = integration;
      }


       public void show() {
           System.out.println("积分:"+integration+",卡类型:"+cardType);
      }
  }
}





 

练习8

代码

package lianxi;

import java.util.Scanner;

public class demo08 {
   public static void main(String[] args) {
       //创建两个管理员对象
       Admin admin1 = new Admin("admin1", "111111");
       System.out.println("姓名:" + admin1.username + ",密码:" + admin1.password);
       Admin admin2 = new Admin("admin2", "222222");
       System.out.println("姓名:" + admin2.username + ",密码:" + admin2.password);
       //创建键盘输入对象
       Scanner scanner = new Scanner(System.in);
       //模仿用户登录输出用户名和密码
       while (true) {
           System.out.println("请输入用户名");
           String usernameIn = scanner.next();
           System.out.println("请输入密码");
           String passwordIn = scanner.next();
           //进行判断是否用户名和密码正确
           if (admin1.username.equals(usernameIn) && admin1.password.equals(passwordIn)
                   || admin2.username.equals(usernameIn) && admin2.password.equals(passwordIn)) {
               System.out.println("登陆成功");
               //修改新密码
               System.out.println("请输入新密码");
               String newPassword = scanner.next();
               //判断是不是admin1
               if (admin1.username.equals(usernameIn)) {
                   admin1.setPassword(newPassword);
                   System.out.println(admin1.username + "新密码为:" + admin1.password);
               break;
              }
               //判断是不是admin2
               if (admin2.username.equals(usernameIn)) {
                   admin2.setPassword(newPassword);
                   System.out.println(admin2.username + "新密码为:" + admin2.password);
               break;
              }

          } else {
               System.out.println("用户名或密码错误,您没有权限更新管理员信息");
               break;
          }
      }

  }


   public static class Admin {
       String username;
       String password;

       public Admin() {
      }

       public Admin(String username, String password) {
           this.username = username;
           this.password = password;
      }

       public String getUsername() {
           return username;
      }

       public void setUsername(String username) {
           this.username = username;
      }

       public String getPassword() {
           return password;
      }

       public void setPassword(String password) {
           this.password = password;
      }
  }

}





 

练习9

代码

package lianxi;

import java.util.Scanner;

public class demo09 {


public static void main(String[] args) {
Menu menu = new Menu();
menu.showLoginMenu();
}

public static class Menu {
//显示登录菜单
Scanner scanner = new Scanner(System.in);

public void showLoginMenu() {
System.out.println("欢迎使用我行我素购物管理系统");
System.out.println("1.登录系统");
System.out.println("2.退出");
System.out.println("---------------");
System.out.println("请输入数字");
int i = scanner.nextInt();
//判断是否为1
if (i == 1) {
showMainMenu();
} else if (i == 2) {
System.out.println("谢谢使用");
} else {
System.out.println("输入错误");
}

}

//显示主菜单
public void showMainMenu() {
System.out.println("我行我素购物管理系统主菜单");
System.out.println("1.客户信息管理");
System.out.println("2.真情回馈");
System.out.println("---------------");
System.out.println("请输入数字");
int i = scanner.nextInt();
//0返回上一级
if (i == 0) {
showLoginMenu();
}
//判断是否为1
else if (i == 1) {
showCustMenu();
} else if (i == 2) {
showSendGMenu();
} else {
System.out.println("输入错误");
}
}

//客户信息管理菜单
public void showCustMenu() {
System.out.println("我行我素购物管理系统");
System.out.println("1.添加");
System.out.println("2.删除");
System.out.println("3.修改");
System.out.println("---------------");
System.out.println("请输入数字");
int i = scanner.nextInt();
//0返回上一级
if (i == 0) {
showMainMenu();
}
//判断是否为1
else if (i == 1) {
System.out.println("执行添加");
showCustMenu();//执行完毕后返回
} else if (i == 2) {
System.out.println("执行删除");
showCustMenu();//执行完毕后返回
} else if (i == 3) {
System.out.println("执行修改");
showCustMenu();//执行完毕后返回
} else {
System.out.println("输入错误");
}

}

//真情回馈菜单
public void showSendGMenu() {
System.out.println("我行我素购物管理系统真情回馈");
System.out.println("1.幸运大转盘");
System.out.println("2.幸运大放送");
System.out.println("3.生日问候");
System.out.println("---------------");
System.out.println("请输入数字");
int i = scanner.nextInt();
if (i == 0) {
showMainMenu();
}
//判断是否为1
else if (i == 1) {
System.out.println("幸运大转盘");
showSendGMenu();//执行完毕后返回
} else if (i == 2) {
System.out.println("幸运大放送");
showSendGMenu();//执行完毕后返回
} else if (i == 3) {
System.out.println("生日问候");
showSendGMenu();//执行完毕后返回
} else {
System.out.println("输入错误");
}
}

}
}





 

练习10

和电脑猜拳,电脑随机输出

人输入数字 1-石头,2-剪刀,3-布

代码

package lianxi;

import java.util.Random;
import java.util.Scanner;

public class demo10 {
public static void main(String[] args) {
//人输入 人输入数字 1-石头,2-剪刀,3-布
Scanner scanner = new Scanner(System.in);
String sr;//人出的是啥
while (true) {
System.out.println("请输入数字");
int i = scanner.nextInt();
if (i == 1) {
sr = "石头";
break;
}
if (i == 2) {
sr = "剪刀";
break;
}
if (i == 3) {
sr = "布";
break;
} else {
System.out.println("输入有误,请重新输入,只可以输入1 2 3");
}
}

//电脑随机出
String[] strings = new String[]{"石头", "剪刀", "布"};
Random random = new Random();
int i = random.nextInt(strings.length);
String sc = strings[i];
System.out.println("人出的"+sr);
System.out.println("电脑出的"+sc);
//判断谁赢
if ("石头".equals(sc) && "剪刀".equals(sr)||"剪刀".equals(sc) && "布".equals(sr)||"布".equals(sc) && "石头".equals(sr)){
System.out.println("电脑赢了");
} else if(sc.equals(sr)){
System.out.println("平手");
} else{
System.out.println("你赢了");
}

}

}





 

 

posted @ 2021-06-19 12:25  想make钱  阅读(68)  评论(0)    收藏  举报