Java--练习题--java从繁琐到更繁琐
1.13,编写一个整数,如果三个数都相等打印equal,否则打印not equal
//1
package main;
import java.util.Scanner;
public class Main {
public static void print(Object ob) {
System.out.print(ob);
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
print("请输入a:");
int a=scanner.nextInt();
print("请输入b:");
int b=scanner.nextInt();
print("请输入c:");
int c=scanner.nextInt();
if(a==b&&b==c) {
print("equal");
}else {
print("not equal");
}
scanner.close();
}
}
//2
package main;
import java.util.Scanner;
public class Main {
private static Scanner sc=new Scanner(System.in);
public static int input_int(String str) {
print(str);
return sc.nextInt();
}
public static void closeScanner() {
sc.close();
}
public static void print(Object ob) {
System.out.print(ob);
}
public static void equal(int a,int b,int c) {
if(a==b&&b==c) {
print("equal");
}else {
print("not equal");
}
}
public static void main(String[] args) {
int a=input_int("请输入a:");
int b=input_int("请输入b:");
int c=input_int("请输入c:");
equal(a, b, c);
closeScanner(); //关闭输入流
}
}
1.15,如果double类型变量x,y都严格位于0,1之间打印true,否则打印false
package main;
import java.util.Scanner;
public class Main {
private static Scanner sc=new Scanner(System.in);
public static double input_double(String str) {
print(str);
return sc.nextDouble();
}
public static void closeScanner() {
sc.close();
}
public static void print(Object ob) {
System.out.print(ob);
}
public static boolean isIn(double x,double y) {
if((x>=0&&x<=1)&&(y>=0&&y<=1)) return true;
else return false;
}
public static void main(String[] args) {
double x=input_double("请输入x:");
double y=input_double("请输入y:");
print(isIn(x, y));
closeScanner(); //关闭输入流
}
}
1.1.9,编写一段代码实现正整数N用二进制表示并转化为一个String类型的值s
//1
Integer.toBinaryString(N)
//2
package main;
public class Main {
public static void print(Object ob) {
System.out.print(ob);
}
public static String toBinaryString(int N) {
String s="";
//从1开始的代码简化
//实现相同工作,代码之间的比较
//1
// int yu=0; //存储余数
// while(N!=0) {
// yu=N%2;
// N=N/2;
// s=yu+s;
// }
//2
// for(int yu=0;N>0;N/=2) {
// yu=N%2;
// s=yu+s;
// }
//3
for(;N>0;N/=2) s=(N%2)+s;
return s;
}
public static void main(String[] args) {
System.out.println(toBinaryString(100));
}
}
Java练习题0x03
最新推荐文章于 2023-07-26 22:35:28 发布