王道入门50题答案
345为进制转换和二进制的反码补码置换,其余不可见图片为代码运行结果,如若决定去王道培训,一下代码仅供参考,看不懂可评论或私信,会回复
1
java是跨平台的,但jvm不是跨平台的。
java编写的文件都会被编译成无关平台的字节码,任何一个平台的字节码都可以在JVM上执行,不同平台的JVM是不同的,jvm提供了相同的接口。
2
配置java环境可以方便使用命令行去编译运行java程序,不配置环境可以使用命令行可以编译代码,但需要在指定目录进行编译。
3
图片转存失败,请去查看其他攻略
图片转存失败,请去查看其他攻略
4
图片转存失败,请去查看其他攻略
5
图片转存失败,请去查看其他攻略
6
不能,因为byte的取值范围是-128~127 300大于此范围,可以把byte改为int或者把b强转为byte
7
a=a+0.1不能通过编译,因为a+0.1这个表达式,会把结果隐式转换为double类型,再赋值给一个int类型的变量会发生错误;a+=0.1可以通过编译,相当于int a=(int)(a+0.1),会丢失精度。
8
不相等,因为其变量类型不同
9
使其整数按位与其减一,为0即为2的整次幂,不为0则不是,
10
字符串,+表示拼接,所以最终类型为字符串
11
结果为20 b=a+10 a=10 b=10+10=20
12
不合法:1myName(数字开头)、你好%(中文)、main、class(关键字)
合法:My_name、 Points、$points、 _sys_ta、 OK123、_23b#、— 3
13
类名:
demo 类名应该开头大写
test 类名应该开头大写
Student
Student_ 不能用_结束
program1 类名应该开头大写
MYDemo 应该使用驼峰命名法MyDemo
myDemo 类名应该开头大写
XueSheng
变量名:
age
nianLing
myName
_name 不能用_下划线开头
Variable
包名:
com.baidu
Com.Baidu 包名要用小写
bao.ceshi
14
注释的作用主要是提高代码的可读性,使其他人能更加轻松地理解和掌握代码。通常用于为语句、程序段、函数等提供解释或提示,帮助读者更好地理解代码的结构和目的。在编程语言中,注释不会影响程序的功能实现,因为编译系统会在编译过程中忽略注释部分。
15
1、导入包import java.util.Scanner;
2、通过new Scanner(System.in)创建一个Scanner,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给Scanner,作为扫描对象;
3、调用Scanner的nextLine()方法;
16
public class huiwen {
public static void main(String[] args) {
int sum =0;
for(int i=10000;i<=99999;i++){
String b = i+"";
if (pd(b)){
sum++;
System.out.print(b+" ");
}
}
}
static boolean pd(String s){
if (s.charAt(0)==s.charAt(4)&&s.charAt(1)==s.charAt(3)){
return true;
}
return false;
}
}
17
import java.util.Scanner;
public class top2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
int n = sc.nextInt();
switch (n){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("您输入的数字不正确,请重新输入");
}
sc.close();
}
}
18
import java.util.Scanner;
public class Top3 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter number");
int number = scan.nextInt();
if (number >= 90) {
System.out.println("优秀");
}else if (number >= 80) {
System.out.println("良好");
}else if (number >= 70) {
System.out.println("中等");
}else if (number >= 60) {
System.out.println("及格");
}else if (number >= 0) {
System.out.println("不及格");
}else {
System.out.println("输入错误,分数不能为负");
}
}
}
19
public class Top4 {
public static void main(String[] args) {
int a,b,c;
for (int i = 100; i <= 999; i++) {
a=i%10;
b=i%100/10;
c=i/100;
if(a*a*a+b*b*b+c*c*c==i){
System.out.println(i);
}
}
}
}
20
public class Top5 {
public static void main(String[] args) {
boolean flag = true;//设置标记位
for (int i = 100; i <= 200; i++){
for (int j = 2; j <= Math.sqrt(i); j++){
if (i%j==0){
flag = false;//false说明不是素数
}
}
if (flag){
System.out.println(i);
flag=true;
}
flag=true;//重置标记进入下轮循环
}
}
}
21
import java.util.Scanner;
public class Top6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int data = sc.nextInt();
for (int i = 1; i <= data; i++) {
for (int k = i + 1; k <= data; k++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
sc.close();
}
}
22
public class Top7 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + "*" + j + "=" + i*j + " ");
}
System.out.println();
}
}
}
23
public class Top8 {
public static void main(String[] args) {
for (int i = 1; i < 1000; i++) {
int sum = 0;
for (int j = 2; j < Math.sqrt(i); j++) {
if (i % j == 0) {
sum+=j+i/j;
}
}
if (sum+1 == i) {//上边因数中排除了1
System.out.println(i);
}
sum = 0;
}
}
}
24
public class top9 {
public static void main(String[] args) {
int[] arr = {1,2,3,4};
int sum=0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i] == arr[j]) {
continue;
}
for (int k = 0; k < arr.length; k++) {
if (arr[i] == arr[k]|| arr[j] == arr[k]) {
continue;
}
System.out.println(arr[i]*100+arr[j]*10+arr[k]);
sum++;
}
}
}
System.out.println(sum);
}
}
25
import java.util.Scanner;
public class Top10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个正整数");
String str = sc.nextLine();
System.out.println(str.length());
for (int i = str.length()-1; i >= 0; i--) {
System.out.print(str.charAt(i));
}
}
}
26
import java.util.Scanner;
public class Top11 {
public static void main(String[] args) {
int a=2,b=1,c;
double ans=0;
for (int i = 0; i < 20; i++) {
c=a;
ans+=(double) a/b;
a=a+b;
b=c;
}
System.out.println(ans);
}
}
27
import java.util.Scanner;
public class Top12 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number: ");
int num = sc.nextInt();
int sum = 0;
for (int i = num; i >= 0; i--) {
sum += i;
}
System.out.println(sum);
}
}
28
import java.util.Scanner;
public class Top13 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number: ");
int num = sc.nextInt();
int sum = 1;
for (int i = num; i > 1; i--) {
sum *= i;
}
System.out.println(sum);
}
}
29
import java.util.Scanner;
public class Top14 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int max=0;
int max1=0,max2=0;
if(a>=b)
{
if(a>=c)
{
max=a;
}
else
{
max=c;
}
}
else
{
if(b>=c)
{
max=b;
}
else
{
max=c;
}
}
System.out.println(+max);
max1=a>b?a:b;
max2=max1>c?max1:c;
System.out.println(+max2);
}
}
30
public class Top15 {
public static void main(String[] args) {
int count=0;
for (int i = 1; i <= 10000; i++) {
int a=(int)Math.sqrt(i+100);
int b=(int)Math.sqrt(i+268);
if (a*a==(i+100)||b*b==(i+268)){
count++;
System.out.println(i);
}
}
System.out.println("一共有"+count+"个数");
}
}
31
public class Top16 {
public static void main(String[] args) {
int a = 0;//存放奇数
int b = 0;//存放偶数
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
b+=i;
}else {
a+=i;
}
}
System.out.println("所有奇数的和为"+a);
System.out.println("所有偶数的和为"+b);
}
}
32
public class Top17 {
public static void main(String[] args) {
double height =100;
double distance = 100;
for (int i = 2; i <= 10; i++) {//首次已加
height = height/2;
distance = distance + height*2;
}
System.out.println(distance);
System.out.println(height);
}
}
33
import java.util.Scanner;
public class Top18 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n%9==0){
int count=0;
while (n>1){
n/=9;
count++;
}
System.out.println(count);
}else {
System.out.println(0);
}
}
}
34
import java.util.Scanner;
public class Top19 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n%2==0){
System.out.println("这是一个偶数");
}else {
System.out.println("这是一个奇数");
}
}
}
35
import java.util.Scanner;
public class tOP20 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.print(n+"=");
int count = 0 ;
for (int i = 2; i <= n; i++) {
if (n % i == 0){//判断时候为因子
count++;
while (n%i ==0){//除掉其中质因数的倍数
n = n/i;
}
if (count==1)System.out.print(i);
else System.out.print("*"+i);
}
}
}
}
36
import java.util.Arrays;
import java.util.Scanner;
public class Top21 {
public static void main(String[] args) {
int[] arr = new int[3];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
for (int i = arr.length-1; i >=0 ; i--) {
System.out.print(arr[i]+" ");
}
}
}
37
import java.util.Scanner;
public class Top22 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(),b = sc.nextInt();
int temp = a;
a = b;
b = temp;
System.out.println(a+" "+b);
}
}
38
import java.util.Scanner;
public class Top23 {
public static void main(String[] args) {
for(int i=1;i<=10000;i++) {
int p = i*i;
String ptr = Integer.toString(p);
String tpr = Integer.toString(i);
String last = ptr.substring(ptr.length()-tpr.length());
if(tpr.equals(last)) {
System.out.println(i);
}
}
}
}
39
public class Top24 {
public static void main(String[] args) {
int a = 20,b=0;
int count =0;
while (a>3){
b=a/3;
count +=b;
a=a-b*3+b;
}
System.out.println(count);
}
}
40
public class Top25 {
public static void main(String[] args) {
double a =56.7,b=0;
int count=0;
while (a>b){
count++;
b+=5;
if (b>a)break;
b-=3.5;
}
System.out.println(count);
}
}
41
import java.util.Scanner;
public class Top26 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
while (n>10) {
sum += n%10;
n/=10;
}
System.out.println(sum+n);
}
}
42
public class Top27 {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 100; i++) {
if (i>=7&&i / 7 == 0 || i % 10 == 7) {
continue;
}
System.out.print(i+" ");
}
}
}
43
import java.util.Scanner;
public class Top28 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double sum = 0;
for (double i = 1; i <= n; i++) {
if (n%2==i%2) {//同为奇数或者同为偶数
sum+=1/i;
}
}
System.out.println(sum);
}
}
44
import java.util.Scanner;
public class Top29 {
public static void main(String[] args) {
int[] arr = {31,29,31,30,31,30,31,31,30,31,30,31};
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int month = sc.nextInt();
if(year%4==0&&year%100==0||year%400==0){
if(month==2){
System.out.println(year+"年的"+month+"月有"+28+"天");
}
}
else {
System.out.println(year+"年的"+month+"月有"+arr[month-1]+"天");
}
}
}
45
import java.util.Scanner;
public class Top30 {
public static void main(String[] args) {
double a = (24+8+3)*0.8;
double b = 16+8+3;
System.out.println(a>b?b:a);
}
}
46
import java.util.Scanner;
public class Top31 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
arr[0] = 1;arr[1]=1;
for (int i = 2; i < n; i++) {
arr[i] =f(i+1);
}
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
static int f(int a){
if (a==1)return 1;
if(a==2)return 1;
return f(a-1)+f(a-2);
}
}
47
import java.util.Scanner;
public class Top32 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
System.out.println(f(n,k-1));
}
static int f(int i, int j){
if (j==0)return i;
return f(i+2,j-1);
}
}
48
import java.util.Arrays;
import java.util.Scanner;
public class Top33 {
public static void main(String[] args) {
int[] arr= new int[10];
Scanner sc = new Scanner(System.in);
for(int i=0;i<arr.length;i++){
arr[i]= sc.nextInt();
}
Arrays.sort(arr);
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
}
49
import java.util.Scanner;
public class Top34 {
static int[] arr = {1,2,3,4,5,6,7,8,0};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == n) {
yd(i);
arr[i] = n;
break;
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
public static void yd(int index) {
for (int i = arr.length-1; i >=index ; i--) {
arr[i] = arr[i-1];
}
}
}
50
import java.util.Scanner;
public class Top35 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();//数组长度
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int max = 0, min = Integer.MAX_VALUE,temp = 0;
for (int i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
for (int i = 0; i < n; i++) {
if (arr[i] == max) {
temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;
}
if (arr[i] == min) {
temp = arr[i];
arr[i] = arr[n-1];
arr[n-1] = temp;
}
}
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}