Java第一课
Posted Vanau
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java第一课相关的知识,希望对你有一定的参考价值。
栗子一:求两个数的最大公约数
import java.util.Scanner;
public class GreatestDivisor
public static void main(String[] args)
Scanner input=new Scanner(System.in);
System.out.print("Please input the number1:");
int number1=input.nextInt();
System.out.print("Please input the number2:");
int number2=input.nextInt();
int gDivisor=Divisor(number1,number2);
System.out.println("The greast divisor is "+gDivisor);
public static int Divisor(int a,int b)
int gd=0,k=1;
while(k<=a&&k<=b)
if((a%k==0)&&(b%k==0))
gd=k;
k++;
return gd;
栗子2:十进制转换为十六进制
import java.util.Scanner;
public class D2H
public static void main(String[] args)
Scanner input=new Scanner(System.in);
System.out.print("Please input your number in decimal:");
int num=input.nextInt();
String str=Dec2Hex(num);
System.out.println("The transformed number "+num+" is:"+str);
public static String Dec2Hex(int deci)
String hex="";
while(deci!=0)
int mod=deci%16;
hex=num2char(mod)+hex;
deci=deci/16;
return hex;
public static char num2char(int nu)
if(nu>=0&&nu<=9)
return (char)(nu+'0');
else
return (char)(nu-10+'A');
栗子三:抓牌游戏
public class DeckofCards
public static void main(String[] args)
int[] deck=new int[52];
String[] suits="Spades","Hearts","Diamonds","Clubs";
String[] ranks="Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King";
for(int i=0;i<deck.length;i++)
deck[i]=i;
for(int i=0;i<deck.length;i++)
int index=(int)(Math.random()*deck.length);
int temp=deck[i];
deck[i]=deck[index];
deck[index]=temp;
for(int i=0;i<4;i++)
String suit=suits[deck[i]/13];
String rank=ranks[deck[i]%13];
System.out.println("Card number "+deck[i]+":"+rank+" of "+suit);
栗子四:统计每个字母出现的次数
import java.util.Scanner;
public class Counts
/**
* @param args
*/
public static void main(String[] args)
// TODO Auto-generated method stub
char[] chars=CreatArray();
System.out.println("The lowercase letters are: ");
DisplayArray(chars);
int[] counts=CountLetters(chars);
System.out.println();
System.out.println("The occurrences of each letter are: ");
DisplayCounts(counts);
public static char[] CreatArray()
Scanner input=new Scanner(System.in);
char[] chars=new char[100];
System.out.print("Please input your string:");
String str=input.next();
chars=str.toCharArray();
return chars;
public static void DisplayArray(char[] chars)
for(int i=0;i<chars.length;i++)
System.out.println(chars[i]);
public static int[] CountLetters(char[] chars)
int[] counts=new int[26];
for(int i=0;i<chars.length;i++)
counts[chars[i]-'a']++;
return counts;
public static void DisplayCounts(int[] counts)
for(int i=0;i<counts.length;i++)
System.out.println(counts[i]+""+(char)(i+'a')+" ");
若在上面处替换为
System.out.println(counts[i]+(char)(i+'a')+" ");
则变为
所以有人能告诉我怎么回事儿吗?
栗子五.文件输入和输出(使用PrintWriter写数据)
File类包含了许多获取文件属性的方法以及重命名和删除文件的方法,也就是说建立的File对象封装了文件和路径的属性,但是,这些方法不包括读写文件内容,使用java.io.PrintWriter
另外还有java.util.Scanner
类能够解决这类问题。
public class WriteData
public static void main(String[] args) throws Exception //这是什么意思我现在还不知道....
java.io.File file=new java.io.File("scores.txt"); //创建file对象
if(file.exists())
System.out.println("File already exists!");
System.exit(0); //System.exit(status),status表示退出的状态码,0表示正常退出,非0表示异常终止。
java.io.PrintWriter output=new java.io.PrintWriter(file); //创建一个PrintWriter实例并且向文件scores.txt写入两行数据
output.println("John 90");
output.println("Tom 88");
output.close(); //文件读写后及时关闭,不然数据会出现异常
或在代码之前跟Scanner一样引入
import java.io.PrintWriter;
//然后在......均可
PrintWriter output=new PrintWriter(file);
以上是关于Java第一课的主要内容,如果未能解决你的问题,请参考以下文章