Posts Tagged ‘Java’

5題迴圈的練習題目

//題目第1題
/*
輸出1+2+3+......+100的總和
*/
public class sn{
  public static void main(String [] args){
  int p=0;
    for(int i=1 ; i<101 ; i++){
    p = p+i;
    }
    System.out.println(p);
  }
}

//題目第2題
/*
印出1~100每個整數 但是跳過尾數為6的整數
*/
public class six{
  public static void main(String [] args){
    for(int i=1 ; i<101 ; i++){
      if(i%10==6){

      }else{
        System.out.print(i+" ");
      }
    }
  }
}

//題目第3題
/*印出
1 2 3 4 ......
1 2 3 4 .....
.
.
.
*/
public class onetwo{
  public static void main(String [] args){
    for(int i=1;i<4;i++){
      for(int q=1; q<4 ;q++){
        System.out.print(q);
      }
      System.out.println();
    }
  }
}

//題目第4題
/*
印出
1
1 2
1 2 3
1 2 3 4 ......
*/

public class sss{
  public static void main(String [] args){
    for(int i=0;i<=10;i++){
      for(int j=1;j<=i;j++){
        System.out.print(j+"  ");
        }
    System.out.println();
    }
  }
}

//題目第5題
/*
九九乘法表
*/
public class nn{
  public static void main(String [] args){
    for(int i=1 ; i<10 ; i++){
      for(int a=2 ; a<10 ; a++){
      System.out.print(a + "x" + i + "=" + a * i +"\t");
      }
      System.out.println();
    }
  }
}

JAVA- 自動約分程式

之前早就想做出來了

可惜程式碼80%都不是我寫的

原本只有約分功能 我把它加進去最大公因數還有最小公倍數的功能

還有輸入的功能

期待以後可以用出個GUI介面 讓一切更方便

檔名:mainclass.java

import java.util.*;
class fenshu
{
  private int up;
  private int up1;
  private int down ;
  private int down1 ;
  public fenshu(int a,int b)
  {
    up=a ;
    down=b ;
  }
  public int getnumber()//求最大公约数
  {
    int min;
    int tem=1;
    min=(up<down)? up:down ;
    for(int i=1;i<=min;i++)
    {
      if(up%i==0&&down%i==0)
      tem=i ;
   
    }
    return tem ;
   
  }
  public void process()
  {
    up1=(int)(up/getnumber()) ;
    down1=(int)(down/getnumber()) ;
  }
  public void showresult()
  {
    int fa = up/up1;
   
    //System.out.println("result:"+up1+"/"+down1) ;
    System.out.println("最大公因數="+fa) ;
    System.out.println("最小公倍數="+fa*up1*down1) ;
    System.out.println("約分的結果"+up1+"比"+down1) ;
    if(up==up1){
      System.out.println("※已經是最簡分數,無法約分!");
    } 
  }
}

public class mainclass
{
  public static void main(String []s)
  {
    System.out.println("請輸入第一個數字");
    Scanner sc = new Scanner(System.in);
    int p = sc.nextInt();
    System.out.println("請輸入第二個數字");
    int q = sc.nextInt();
    if (q>p)
    {
    int tmp1=0;
    int tmp2=0;
    tmp1 = p;
    tmp2 = q;
    p = tmp2;
    q = tmp1;
    }
    fenshu fs=new fenshu(p,q) ;
    
    fs.process() ;
    fs.showresult() ;
   
  }
}

JAVA-你好範例以及老師範例

原本想說弄個丁丁說你好的程式,之後就做成這樣子了。

import java.util.*;
public class test{
  public static void main(String [] args){
    System.out.println("請輸入您想說的話");
    Scanner sc = new Scanner(System.in);
    String x = sc.nextLine();
    System.out.println("請輸入您想重複的次數");
    int q = sc.nextInt();
    System.out.println("是否換行,是請輸入1,否請輸入2");
    int a = sc.nextInt();
    System.out.println("是否顯示次數,是請輸入1,否請輸入2");
    int b = sc.nextInt();
    if(a==1&&b==1){  
    for(int i=0;i<q;i++){ //是是
      int z = i+1;
      System.out.println(x + "  (這是第" + z + "次)");
    }
    }//end if
    if(a==1&&b==2){
    for(int i=0;i<q;i++){ //是否
      int z = i+1;
      System.out.println(x);
    }
    }//end if
    if(a==2&&b==1){
    for(int i=0;i<q;i++){ //否是
      int z = i+1;
      System.out.print(x + "  (這是第" + z + "次)");
    }
    }//end if
    if(a==2&&b==2){
    for(int i=0;i<q;i++){ //否否
      int z = i+1;
      System.out.print(x);
    }
    }//end if
    else{
    System.out.println("輸入錯誤程式結束!");
    }
  } 
}

下面兩個是一組的,要一起使用喔,(範例:老師)

public class Teacher{
  private String name;
  public Teacher(String name){
    this.name = name;
  }
  public void teach(){
    System.out.println(this.name + "正在教學");
  }
  public String getName(){
    return this.name;
  }
}

import java.util.*;
public class TestTeacher{
  public static void main(String [] args){
    System.out.println("請輸入老師姓名");
    Scanner sc = new Scanner(System.in);
    //String a = s.nextString();
    String a = sc.nextLine();
    Teacher teacher1 = new Teacher(a);
    System.out.println("老師姓名;"  + teacher1.getName());
    teacher1.teach();
   
  }
}

可拖拉按鈕

可以拖拉之後顯示滑鼠位置

可以讓人理解 電腦中X軸右+左- Y軸下+上- (我沒搞錯跟數學的不一樣)

package dragged;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestMouseDragged{
int x,y ;
  int x0,y0 ;
  JButton btn=new JButton("可拖拉許功蓋");
  public TestMouseDragged(){
 
   
    JFrame f = new JFrame("許功蓋");
    f.setLayout(new FlowLayout(FlowLayout.CENTER));
   
    btn.addMouseListener(new MouseAdapter(){
   
      public void mouseClicked(MouseEvent me){
        x0 = me.getX();
        y0 = me.getY();
        System.out.println("mouse click-x0="+x0+"y0="+y0);
      }
      });  
      btn.addMouseMotionListener(new MouseAdapter(){
        public void mouseDragged(MouseEvent me){
          int x = btn.getX()+me.getX() - x0;
          int y = btn.getY()+me.getY() - y0;
          btn.setLocation(x,y);
          System.out.println("mouse X位置="+x+"Y位置"+y);   
        }
      });
      f.add(btn);
      f.pack();
      f.setSize(600,600);
      f.setVisible(true);
     
   

  }
      public static void main(String [] args){
      new  TestMouseDragged();
      }
}

 

JAVA IO

兩個很好玩的JAVA

看來IO真的很好玩

讀取檔案

package iostream;
import java.io.*;
public class FileIOStream{
  public static void main(String [] args)throws IOException{
    FileInputStream fis = new FileInputStream("FileIOStream.java");//輸入檔案的名稱
    byte[] b = new byte[fis.available()];
    fis.read(b);
    FileOutputStream fos = new FileOutputStream("FileIOStream2.java");//輸出檔案的名稱
    fos.write(b);
    fos.flush();
    fos.close();
    fis.close();
  }
}

package reader;
import java.io.*;
public class FileIOReader {
  public static void main(String [] args)throws IOException{
    FileReader fr = new FileReader("FileIOStream.java");//要讀取的檔案
    BufferedReader br = new BufferedReader(fr);
    String temp = null ;
    while((temp = br.readLine())!=null)
    System.out.println(temp);
    br.close();
  } 
}

JAVA-interfacesDEMO

package _class; import _interfaces.*; public class InterfaceDemoImpl implements InterfaceDemo{ public void testA(){ System.out.println("testA()"); } public String testB(){ return "testB()"; } public static void main(String [] args){ InterfaceDemoImpl i=new InterfaceDemoImpl(); i.testA(); System.out.println(i.testB()); //InterfaceDemoImpl.a=10000; //以上註解拿掉就會過不了 } }

JAVA-CustomException

package exception;

class CustomException extends Exception{
public CustomException(){
    super("自訂例外");
}
public CustomException(String msg){
    super(msg);
}
}

public class TestCustomException{
public static void main(String [] args){

try{
  throw new CustomException();
}catch (CustomException ce ){
ce.printStackTrace();
}
}
}

JAVA-Exception

package exception; import java.util.Scanner ; import java.io.*; class ExceptionFather{ public String readString() throws FileNotFoundException{ Scanner s = new Scanner (new File("input.txt")); return "ExceptionFather -"+ s.next() ; } } class ExceptionSon extends ExceptionFather{ @Override public String readString() { Scanner s=null; String temp=null; try{ s=new Scanner(new File ("input.txt")); temp="ExceptionSon-"+s.next(); }catch (FileNotFoundException fnfe ){ System.out.println("例外="+fnfe.toString()); }finally { System.out.println("Finally"); } return temp; } } public class TestException{ public static void main(String [] args){ ExceptionSon s=new ExceptionSon(); System.out.println(s.readString()); try{ ExceptionFather f=new ExceptionFather(); System.out.println(f.readString()); }catch(FileNotFoundException fnfe){ fnfe.printStackTrace(); } } }

Java 開始人類聽雷

我不是鴨子...........

從SL100最後幾次就開始聽雷了

現在SL275連雷聲都聽不見了

陣列 ststic new 繼承 封裝...........................................

Java很方便但是現在負擔好大阿

Java sl275 c2

第一個

package wrapper;
import java.util.*;
public class WrapperDemo{
  public static void main(String [] args ){
  System.out.println("Andy 很師?(true/false)");
  Scanner s=new Scanner (System.in);
  if(new Boolean (s.next( )).booleanValue())
    System.out.println("你答對了!");
    else
     System.out.println("你答錯了!");
     }
    }

第二個

package imit;
public class InitFlowDemo{
  int var =100;
  {
  System.out.println("[全域變數="+ var +"]");//第二出來
  }
  public  InitFlowDemo(){
  System.out.println("[建構子]");//第三出來
  }
  public static void main(String [] args ) {
    System.out.println("[before new InitFlow ]");//最先出來
  new InitFlowDemo().method();
  }
  public void method(){
  System.out.println("[方法呼叫]");//第四出來
  }
  }