package com.woniu.day03.test;
public class HomeWork {
public static void main(String[] args) {
/
阅读以下代码,判断错误,并说出原因,如何修正
int a=10;
char b=a;
/
/int a=10;
char b=(char) a;/
//阅读以下代码,判断错误,并说出原因,如何修正
int a=10;
char sex=’1’;
//char b=(char)(a+sex);//输出结果
}
/public static void main(String[] args) {
* 编写一个Java程序要求如下
a)定义:int类型变量a,long类型变量b,double类型变量c,float变量 d<br /> b)变量a赋值10,b赋值8997689076,c赋值5.98,d赋值3.68<br /> c)定义long类型变量s1,为其赋值a+b<br /> d)定义double类型变量s2,为其赋值a+b*c+d<br /> e)定义int类型变量变量 s3,将s2强制转换为int类型数据赋值于s3。<br /> f)定义long类型变量变量 s4,为其赋值a与b之商。<br /> g)定义long类型变量变量 s5,为其赋值a除以b的余数。<br /> h)打印a,b,c,d,s1,s2,s3,s4,s5<br /> <br /> int x=1,y=2,z=5 ;<br /> x = 10;y = 100 ;z=44;<br /> <br /> int a = 10;<br /> long b = 8997689076L;<br /> double c = 5.98 ;<br /> float d = 3.68F;<br /> <br /> long s1 = a + b ;<br /> double s2 = a+b*c+d ;<br /> <br /> int s3 = (int) s2;<br /> long s4 = a/b ;<br /> <br /> long s5 = a%b ;<br /> <br /> <br /> }*/<br /> <br /> <br /> /*public static void main(String[] args) {<br /> <br /> * 编写一个Java程序要求如下:<br /> a)定义两个int类型变量a和b,并分别赋初值15和18<br /> b)交换两个变量的值。<br /> c)打印两个变量的值。<br /> <br /> int a = 100 ;<br /> int b = 200 ;<br /> <br /> System.out.println("a="+a +"---b="+b);<br /> <br /> int temp = a ;<br /> a = b ; //把b的赋值给a <br /> b = temp ;<br /> <br /> System.out.println("a="+a +"---b="+b);<br /> }*/<br />}
package com.woniu.day04.homework;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
//键盘输入两个数,求出最大值和最小值?(if 和三元运算符两种方式实现)
//创建一个Scanner对象
Scanner sc = new Scanner(System.in);
//nextInt 只接受数字
System.out.println(“请输入对一个数字:”);
int num1 = sc.nextInt();
System.out.println(“请输入第二个数字:”);
int num2 = sc.nextInt();
//System.out.println(num1 +”—-“+num2);
int max = num1 > num2 ? num1 :num2 ;
System.out.println(“max —->”+max);
int max2 ; //变量的生命
//System.out.println(max2);
//max2 = 0;
if(num1 > num2) {
max2 = num1;
}else {
max2 = num2;
}
System.out.println(“max2——>”+max2);
}
}
package com.woniu.day04.homework;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
//创建一个Scanner对象
Scanner sc = new Scanner(System.in);
//nextInt 只接受数字
System.out.println(“请输入对一个数字:”);
int num1 = sc.nextInt();
System.out.println(“请输入第二个数字:”);
int num2 = sc.nextInt();
System.out.println(“请输入第三个数据:”);
int num3 = sc.nextInt();
int max = (num1 > num2?num1:num2) > num3 ? (num1 > num2?num1:num2) :num3;
System.out.println(“max—-“+max);
int x ;
if(num1 > num2) {
x = num1;
}else {
x = num2;
}
if(x < num3) {
x = num3 ;
}
System.out.println(“x—-“+x);
}
}
package com.woniu.day04.homework;
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
//键盘输入一个数,判断是否是奇偶数?(if 和三元运算符两种方式实现)
Scanner sc = new Scanner(System.in);
System.out.println(“请输入一个数据:”);
int data = sc.nextInt();
String s = data % 2 == 0 ?”偶数”:”奇数”;
System.out.println(s);
if(data % 2 == 0){
System.out.println(“偶数”);
}else {
System.out.println(“奇数”);
}
}
}
package com.woniu.day04.homework;
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“请输入一个字符:”);
String s = sc.next();
//把字符串变成字符
char c = s.charAt(0);
if(c >= ‘A’ && c <= ‘Z’) {
System.out.println(c + “是 大写”);
}else if(c >= ‘a’ && c <= ‘z’){
System.out.println(c +”是小写字母”);
}else if(c >= ‘0’ && c <= ‘9’){
System.out.println(c +”是一个数字!”);
}else {
System.out.println(“输入错误!”);
}
}
}
package com.woniu.day04.homework;
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
//键盘输入一个年份,判断是否是闰年?,如果是闰年则输出该年是闰年,否则输出该年是平年?
Scanner sc = new Scanner(System.in);
System.out.println(“请输入年份:”);
int year = sc.nextInt();
if((year % 4 ==0 && year % 100 !=0) || year % 400 ==0 ){
System.out.println(year + “是闰年!”);
}else {
System.out.println(year + “是平年!”);
}
}
}
package com.woniu.day04.homework;
import java.util.Scanner;
public class Test6 {
public static void main(String[] args) {
/
键盘输入一个百分制分数,判断分数的等级,分数对应等级关系如下:
A[90,100] B[80,90) C[70,80) D[60,70) E[0,60)
/
Scanner sc = new Scanner(System.in);
System.out.println(“请输入分数:”);
int score = sc.nextInt();
switch (score / 10) {
case 10:
System.out.println(“A”);
break;
case 9:
System.out.println(“A”);
break;
case 8:
System.out.println(“B”);
break;
case 7:
System.out.println(“C”);
break;
case 6:
System.out.println(“D”);
break;
default:
System.out.println(“E”);
break;
}
}
}
package com.woniu.day04.homework;
import java.util.Scanner;
public class Test8 {
public static void main(String[] args) {
//请输入 年月日 计算 是本年度的第几天
Scanner sc = new Scanner(System.in);
System.out.println(“请输入年份:”);
int year = sc.nextInt();
System.out.println(“请输入月份:”);
int month = sc.nextInt();
System.out.println(“请输入日期:”);
int day = sc.nextInt();
int sum = 0 ;// 存储本年度度过的天数
switch (month-1) {
case 11:
sum += 30;
case 10:
sum += 31;
case 9:
sum += 30;
case 8:
sum += 31;
case 7:
sum += 31;
case 6:
sum += 30;
case 5:
sum += 31;
case 4:
sum += 30;
case 3:
sum += 31;
case 2:
if((year % 4 ==0 && year % 100 !=0 ) || year % 400 == 0){
sum += 29;
}else {
sum += 28;
}
case 1:
sum += 31;
break;
default:
break;
}
System.out.println(“到本月底【”+month+”】已经度过了【”+(sum+day)+”】天.”);
//从键盘输入一个年份和月份,计算出本年度已经度过多少天 ?
/Scanner sc = new Scanner(System.in);
System.out.println(“请输入年份:”);
int year = sc.nextInt();
System.out.println(“请输入月份:”);
int month = sc.nextInt();
int sum = 0 ;// 存储本年度度过的天数
switch (month) {
case 12:
sum += 31;
case 11:
sum += 30;
case 10:
sum += 31;
case 9:
sum += 30;
case 8:
sum += 31;
case 7:
sum += 31;
case 6:
sum += 30;
case 5:
sum += 31;
case 4:
sum += 30;
case 3:
sum += 31;
case 2:
if((year % 4 ==0 && year % 100 !=0 ) || year % 400 == 0){
sum += 29;
}else {
sum += 28;
}
case 1:
sum += 31;
break;
default:
break;
}
System.out.println(“到本月底【”+month+”】已经度过了【”+sum+”】天.”);/
}
}
package com.woniu.day05.homework;
import java.util.Random;
import java.util.Scanner;
public class HomeWork {
//打印一个数字区间的值
public static void work1(int start,int end){
for(int i=start;i<=end ;i++){
System.out.println(i);
}
}
//打印 100—-1
public static void work2(int start ,int end){
for(int i=start;i>=end ;i—){
System.out.println(i);
}
}
//打印100内的等差数列,从1开始,差值为4
public static void work3(int num,int k){
for(int i=1;i<=num;i+=k){
System.out.println(i);
}
}
//打印100内的等比数列,从1开始,等比值为2
public static void work4(int num ,int k){
for(int i=1;i<= num ;i = k){
System.out.println(i);
}
}
//输出100内能被3整除但是不能被4整除的所有奇数?
public static void work5(){
for(int i=1;i<=100;i++){
if(i % 3 == 0 && i % 4 != 0 && i % 2 != 0){
System.out.println(i);
}
}
}
//求100内的和 并且 返回出去
public static int work6(int num){
int sum = 0 ;
for(int i=0;i<=num;i++){
sum += i;
}
return sum ;
}
//求某个数据内的和 并且 返回出去 1+2+3+…+100=5050
public static String work6_bak(int num){
int sum = 0 ;
String str = “” ;
for(int i=1;i<=num;i++){
sum += i;
if(i != num){
str += i +”+” ;
}else {
str += i ;
}
}
str += “=”+sum;
return str ;
}
//一个循环求出100内的奇数和、偶数和
public static void work7(){
int evenSum = 0 ;//偶数和
int oddSum = 0 ; //奇数和
for(int i=1;i<=100;i++){
if(i % 2 == 0){
evenSum += i;
}else {
oddSum += i;
}
}
System.out.println(“偶数和—>”+evenSum +”—-奇数和—>”+oddSum);
}
//求6!
public static void work8(int num){
int sum = 1;
for(int i=num;i>0;i—){
sum = i;
}
System.out.println(num+”!=”+sum);
}
//产生一个由数字,字母组成的6位随机验证码 允许重复
public static String work9(int num){
String s = “0123456789QWERTYUIOPLKJHGFDSAZXCVBNMqazwsxedcrfvtgbyhnujmiklop”;
//System.out.println(s.length());
String code = “”; //保存生成的随机数
//随机生成一个 0—-62 之间的一个随机数
Random r = new Random();
//连续生成 num 次
for(int i=0;i
//根据这个随机数 在 字符串 s 中获取一个对应下标的字符
char c = s.charAt(index);
//System.out.println(c);
code += c;
}
return code;
}
//产生一个由数字,字母组成的6位随机验证码 不允许重复
public static String work9_bak(int num){
String s = “0123456789QWERTYUIOPLKJHGFDSAZXCVBNMqazwsxedcrfvtgbyhnujmiklop”;
String code = “”; //保存生成的随机数
//随机生成一个 0—-62 之间的一个随机数
Random r = new Random();
for(;;){
//当验证码达到 要求的为数 num 就终止循环
if(code.length() == num){
break;
}
//生成随机 获取 一个验证的字符
int i = r.nextInt(62);
char c = s.charAt(i);
//判断生成的字符是否和 验证码字符串重点字符重复
boolean flag = checkCode(code, c);
if(flag == true){
continue ;
}else {
code += c;
}
}
return code ;
}
/
判断一个字符 是否在 一个字符串中存在
/
public static boolean checkCode(String code , char c){
for(int i=0;i
return true ;
}
}
return false ;
}
/
* 编写一个猜拳游戏,进入首页时打印出页面 (选做题)
———- 欢迎进入猜拳游戏 ————
1. 剪刀
2. 石头
3. 布
———请玩家按对应数字完成出拳 ——
要求 : 电脑需要随机出拳(随机数), 用户键盘出拳后,系统最终要返回输赢结果(电脑赢,玩家赢,平局)<br /> */<br /> <br /> public static void work10(){<br /> Scanner sc = new Scanner(System.in);<br /> <br /> //电脑随机出一个拳 <br /> Random r = new Random();<br /> int m = r.nextInt(3) + 1;<br /> <br /> initMenu();<br /> <br /> int player = sc.nextInt();<br /> <br /> //判断输赢<br /> if(m == 1){<br /> if(player == 1){<br /> System.out.println("平局");<br /> }else if(player == 2){<br /> System.out.println("恭喜你,你赢了!");<br /> }else if(player == 3){<br /> System.out.println("笨蛋,你输了");<br /> }else {<br /> System.out.println("数据输入错误,GG!");<br /> }<br /> }else if(m == 2){<br /> if(player == 1){<br /> System.out.println("笨蛋,你输了");<br /> }else if(player == 2){<br /> System.out.println("平局");<br /> }else if(player == 3){<br /> System.out.println("恭喜你,你赢了!");<br /> }else {<br /> System.out.println("数据输入错误,GG!");<br /> }<br /> }else {<br /> if(player == 1){<br /> System.out.println("恭喜你,你赢了!");<br /> <br /> }else if(player == 2){<br /> System.out.println("笨蛋,你输了");<br /> <br /> }else if(player == 3){<br /> System.out.println("平局");<br /> }else {<br /> System.out.println("数据输入错误,GG!");<br /> }<br /> }<br /> <br /> }<br /> <br /> //封装游戏菜单<br /> public static void initMenu(){<br /> System.out.println("------- 欢迎进入猜拳游戏 --------");<br /> System.out.println(" 1. 剪刀 ");<br /> System.out.println(" 2. 石头 ");<br /> System.out.println(" 3. 布 ");<br /> System.out.println("------请玩家按对应数字完成出拳 ----");<br /> }
<br /> public static void main(String[] args) {<br /> <br /> work10();<br /> <br /> //String s = work9_bak(6);<br /> //System.out.println(s);<br /> <br /> //String s = work9(6);<br /> //System.out.println(s);<br /> <br /> //work1(1,20);<br /> <br /> //work2(20,10);<br /> <br /> //work3(100,8);<br /> <br /> //work4(100,4);<br /> <br /> //work5();<br /> <br /> //int sum = work6(50);<br /> //System.out.println(sum);<br /> <br /> //String s = work6_bak(100);<br /> //System.out.println(s);<br /> <br /> //work7();<br /> <br /> //work8(20);<br /> }<br /> <br />}