Java基础

1显示时间(DisplayTime)


import java.util.Scanner;



public class DisplayTime {



public static void main(String[] args) {

try (Scanner in = new Scanner(System.in)) {

int seconds = in.nextInt();

int hour = seconds/3600;

int leftSeconds = seconds - 3600*hour;

int min = leftSeconds/60;

int sec = leftSeconds%60;

System.out.printf("%02d:%02d:%02d",hour,min,sec);

}

}

}

//hour*100/100+":"+min*100/100+":"+sec*100/100



2华氏温度转摄氏温度(FahrenheitToCelsius)


import java.util.Scanner;



public class FahrenheitToCelsius {



public static void main(String[] args) {

try (// TODO Auto-generated method stub

Scanner input = new Scanner(System.in)) {

System.out.printf("请输入华氏温度\n");

double fahrenheit = input.nextDouble();

double celsius = (fahrenheit - 32)*(5.0/9);

System.out.printf("摄氏温度为:%.2f\n",celsius);

}

}



}

3分段计算水费(Water)


import java.util.Scanner;



public class Water {



public static void main(String[] args) {

try (// TODO Auto-generated method stub

Scanner inpute = new Scanner(System.in)) {

System.out.println("请输入x的值:");

double x = inpute.nextDouble();

if(x <= 15) {

System.out.printf("%.2f\n",4.0*x/3);

}else {

System.out.printf("%.2f\n",2.5*x-10.5);

}

}



}



}

4冒泡排序(BubbleSort)


package homework;



import java.util.Scanner;



public class BubbleSort {



public static void main(String[] args) {

int []a = new int [10];

System.out.println("请输入10个无序数");

try (Scanner input = new Scanner (System.in)) {

for(int i=0;i<a.length;i++) {

int value = input.nextInt();

a[i] = value;

}

}

for(int i=0;i<a.length;i++) {

for(int j=i+1;j<a.length;j++) {

if(a[i]>a[j]) {

int temp = a[i];

a[i]=a[j];

a[j]=temp;

}

}

}

for(int i=0;i<a.length;i++) {

System.out.printf(a[i] + " ");

}

}

}

5求两个数的最大公约数(CommonDivisor)


package homework;



import java.util.Scanner;



public class CommonDivisor {



public static void main(String[] args) {

System.out.println("请输入两个数:");

try (Scanner input = new Scanner (System.in)) {

int n1=input.nextInt();

int n2=input.nextInt();

System.out.println(commondivisor(n1,n2));

}

}

public static int commondivisor(int n1,int n2) {

int temp=0;

for(int i=1;i<=n1 && i<=n2;i++) {

if(n1%i == 0 && n2%i ==0) {

temp=i;

}

}

return temp;

}

}

6生成0~99随机数(RandomDemo)


package homework;

import java.util.Random;

public class RandomDemo {



public static void main(String[] args) {

Random r = new Random();

int number = r.nextInt(100);

System.out.println(number);

}



}

7判断回文串(CheckPalindrome)


package homework;



import java.util.Scanner;



public class CheckPalindrome {



public static void main(String[] args) {

System.out.println("请输入一个字符串:");

try (Scanner input = new Scanner(System.in)) {

String s0 = input.next();

if(s0.length() % 2 == 0) {

StringBuilder s1 = new StringBuilder(s0.substring(0, s0.length()/2));

StringBuilder s2 = new StringBuilder(s0.substring(s0.length()/2, s0.length()));

s2.reverse();

if(String.valueOf(s1).equals(String.valueOf(s2))) {

System.out.println("这是一个回文串");

}else {

System.out.println("这不是一个回文串");

}

}else {

StringBuilder s1 = new StringBuilder(s0.substring(0, s0.length()/2));

StringBuilder s2 = new StringBuilder(s0.substring(s0.length()/2 + 1, s0.length()));

s2.reverse();

if(String.valueOf(s1).equals(String.valueOf(s2))) {

System.out.println("这是一个回文串");

}else {

System.out.println("这不是一个回文串");

}

}

}

}

}

8记事本(NoteBook)


package notebook;



import java.util.ArrayList;



public class NoteBook {



private ArrayList<String> notes = new ArrayList<String>();

public void add(String s) {

notes.add(s);

}



public int getSize() {

return notes.size();

}



public String getNote(int index) {

return notes.get(index);

}



public void removeNote(int index) {

notes.remove(index);

}



public String[] list() {

String[] a = new String[notes.size()];

notes.toArray(a);

return a;

}



public static void main(String[] args) {

NoteBook notebook = new NoteBook();

notebook.add("NO.1");

System.out.println(notebook.getNote(0));

notebook.add("No.2");

System.out.println(notebook.getNote(1));

notebook.removeNote(1);

System.out.println(notebook.getSize());

String[] a = notebook.list();

for(int i=0; i<notebook.getSize(); i++) {

System.out.println(a[i]);

}

}

}