Code
程序员文章站
2022-03-26 17:48:35
Ctrl+c / v 库...
代码
输出
public class One{
public static void main(String[] args){
System.out.println("hello world");
}
}
键盘输入
import java.util.Scanner;
public class Two{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please input data = ");
double x = sc.nextDouble();
System.out.println("Please input character = ");
string y = sc.next();
System.out.println(y + x);
}
}
if
import java.util.Scanner;
public class Three{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please input mouth = ");
int mouth = sc.nextInt();
if(mouth >= 3 && mouth <= 5){
System.out.println("Spring");
}else if(mouth >= 6 && mouth <= 8){
System.out.println("Summer");
}else if(mouth >= 9 && mouth <= 11){
System.out.println("Autumn");
}else if(mouth == 12 || mouth ==1 || mouth == 2){
System.out.println("Winter");
}else{
System.out.println("Error");
}
}
}
switch
import java.util.Scanner;
public class Four{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please input mouth = ");
int mouth = sc.nextInt();
switch(mouth){
case 3:
case 4:
case 5:
System.out.println("Spring");
break;
case 6:
case 7:
case 8:
System.out.println("Summer");
break;
case 9:
case 10:
case 11:
System.out.println("Autumn");
break;
case 12:
case 1:
case 2:
System.out.println("Winter");
break;
default:
System.out.println("Error");
break;
}
}
}
比较大小
import java.util.Scanner;
public class Five{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Piease input x = ");
double x = sc.nextDouble();
System.out.println("Please input y = ");
double y = sc.nextDouble();
System.out.println("Please input z = ");
double z = sc.nextDouble();
if(x > y){
if(x > z)
System.out.println("max = " + x);
else
System.out.println("max = " + z);
}else{
if(y > z)
System.out.println("max = " + y);
else
System.out.println("max = " + z);
}
}
}
for
import java.util.Scanner;
public class Six{
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i <= 100; i++){
if(i % 2 == 1){
sum += i;
}
}
System.out.println(sum);
}
}
计数器
import java.util.Scanner;
public class Seven{
public static void main(String[] args) {
int count = 0;
for(int i = 100; i <= 999; i++){
int a = i % 10;
int b = i / 10 % 10;
int c = i / 100;
if(a * a * a + b * b * b + c * c * c == i){
System.out.println(i);
count++;
}
}
System.out.println(count);
}
}
while
import java.util.Scanner;
public class Eight{
public static void main(String[] args) {
int count = 0;
int i = 100;
while(i <= 999){
int a = i % 10;
int b = i / 10 % 10;
int c = i / 10 / 10 % 10;
if (a * a * a + b * b * b + c * c* c == i){
System.out.println(i);
count++;
}
i++;
}
System.out.println(count);
}
}
do while
import java.util.Scanner;
public class Nine{
public static void main(String[] args) {
int count = 0;
int i = 100;
do{
int a = i % 10;
int b = i / 10 % 10;
int c = i / 10 / 10 % 10;
if (a * a * a + b * b * b + c * c* c == i) {
System.out.println(i);
count++;
}
i++;
}
while(i <= 999);
System.out.println(count);
}
}
无限循环
import java.util.Scanner;
public class Ten{
public static void main(String[] args) {
while(ture){
System.out.println("Over");
}
}
}
import java.util.Scanner;
public class Ten{
public static void main(String[] args) {
if(;;){
System.out.println("over");
}
}
}
for嵌套
import java.util.Scanner;
public class Eleven{
public static void main(String[] args) {
for(int i = 1; i <= 9; i++){
for(int j = 1; j <= i; j++){
int k = i * j;
System.out.print(i + "*" + j + "=" + k + '\t');
}
System.out.println();
}
}
}
重载方法
public class Twelve {
public static void main(String[] args){
boolean x = isEquals(20,30);
System.out.println(x);
}
public static boolean isEquals(int a, int b){
return a == b;
}
public static boolean isEquals(double a, double b){
return a == b;
}
}
一维数组遍历
public class Thirteen{
public static void main(String[] args){
int[] arr1 = new int[5];
arr1[0] = 1;
arr1[1] = 2;
arr1[2] = 3;
arr1[3] = 4;
arr1[4] = 5;
print(arr1);
System.out.println("---------");
int[] arr2 = {11,22,33,44,55};
print(arr2);
}
public static void print(int[] arr){
for(int i = 0; i < arr.length; i++){
System.out.println(arr[i]);
}
}
}
二维数据遍历
public class Fourteen{
public static void main(String[] args){
int[][] arr1 = new int[3][2];
arr1[0][0] = 1;
arr1[0][1] = 2;
arr1[1][0] = 3;
arr1[1][1] = 4;
arr1[2][0] = 5;
arr1[2][1] = 6;
int[][] arr2 = {{1,2,3},{4,5},{6,7,8,9}};
print(arr1);
System.out.println("---------");
print(arr2);
}
public static void print(int[][] arr){
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
整数反转
import java.util.Scanner;
public class Solution1 {
public static void main(String[] args){
long boundary1 = (long)Math.pow(2, 31); //改变溢出值在此更改boundary1
long boundary2 = boundary1 - 1;
Scanner sc = new Scanner(System.in);
System.out.println("Please input digit(" + -boundary1 + "~" + boundary2 + ") = ");
long digit = sc.nextLong();
int digits = 1;
for(int i = 0; i < 10; i++){ //改变溢出值位数在此更改i
long divisor = (long)Math.pow(10, i);
if(digit > -10 * divisor && digit < 10 * divisor){
break;
} else{
digits++;
}
}
if(digit > -boundary1 && digit < boundary2) {
long rvDigit = reverse(digit, digits);
if(rvDigit > -boundary1 && rvDigit < boundary2){
System.out.println(rvDigit);
}else{
System.out.println("0");
}
}else{
System.out.println("Illegal character");
}
}
public static long reverse(long x, int y){
long arr1[] = new long[y];
for(int i = 0; i < y; i++){
long divisor = (long)Math.pow(10, i);
arr1[i] = x / divisor % 10;
}
reverseArr(arr1);
long z = 0;
for(int i = 0; i < y; i++){
long divisor = (long)Math.pow(10, i);
z += arr1[i] * divisor;
}
return z;
}
public static void reverseArr(long arr[]){
for(int i = 0; i < arr.length / 2; i++){
long temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
}
}
private封装
public class Fifteen {
public static void main(String[] args){
Person p =new Person();
p.setName("张三");
p.getName();
p.setAge(20);
p.getAge();
p.setMale(false);
p.getMale();
p.speak();
}
}
class Person {
private String name;
private int age;
private boolean male;
public void speak(){
if(age != 0) {
if (male == true) {
System.out.println("Name is " + name + "; Age is " + age + "; Man");
} else {
System.out.println("Name is " + name + "; Age is " + age + "; Woman");
}
}else{
System.out.println("No answer");
}
}
public void setAge(int age){
if(age > 0 && age < 121){
this.age = age;
}else{
System.out.println("Illegal character");
}
}
public int getAge(){
return age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setMale(boolean male){
this.male = male;
}
public boolean getMale(){
return male;
}
}
本文地址:https://blog.csdn.net/weixin_54231215/article/details/112168325
推荐阅读
-
VSCODE添加open with code实现右键打开文件夹
-
C#生成Code39条形码而非条形码字体的方法
-
解决Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-f8IeEI/MYSQL-python/
-
Entity Framework Code First属性映射约定
-
记录微信支付开发中的小经验(errcode = 40163; errmsg = "code been used")
-
利用Typings为Visual Studio Code实现智能提示功能
-
全新Visual Studio Code预览版0.10.10发布下载
-
Let's Code
-
ubuntu下VS code如何调试C++代码
-
详解VS Code使用之Vue工程配置format代码格式化