示例:
public class study01 {
public static void farmer(){
System.out.println("播种");
System.out.println("浇水");
}
public static void seller(){
System.out.println("施肥");
}
public static void me(){
System.out.println("吃");
}
public static void main(String[] args){
//农民
farmer();
//商贩
seller();
//我
me();
}
}
println与print的区别:前者自动换行
javac与java的区别:java执行的是class文件,所以需要javac把java文件转换成class文件。
示例2:
public class study01 {
public static void main(String[] args){
printMethod();
}
public static void printMethod(){
for (int j=0; j< 5; j++)
{
for (int i=0; i<20; i++)
{
System.out.print("*");
}
System.out.println();
}
}
}
示例三:
public class study01 {
public static void main(String[] args){
int num = sum(34,20);
System.out.println("变量的值为:"+num);
}
public static int sum(int a, int b) {
int result = a+b;
return result;
}
}
示例四:
public class study01 {
public static void main(String[] args){
System.out.println(isSame(11,10));
}
public static boolean isSame(int a, int b){
// boolean same = a == b ? true : false;
// return same;
return a == b;
}
}
示例五:任意两个数之间的连续相加和
public class study01 {
public static void main(String[] args){
System.out.println(sum(2,100));
}
public static int sum(int a, int b){
int c = 0;
for (int i = a; i <= b; i++){
c = c + i;
}
return c;
}
}