2014年12月26日 星期五

java程式設計題目---字母處理【輸出字母、換行(使用method)】

Write a method that prints characters using the following header:
public static void printChars(char ch1, char ch2, int numberPerLine)
This method prints the characters between ch1 and ch2 with the specified numbers per line. Write a test program the prints ten characters per line from 1 to Z. Character are separated by exactly one space.





import java.util.Scanner;

public class C1
{
 public static void main(String[] args) 
 {
  Scanner input = new Scanner(System.in);
  
  //提示使用者輸入
  System.out.println("請輸入起始字元:");
  char startchar = input.next().charAt(0);
  System.out.println("請輸入結束字元:");  
  char endchar = input.next().charAt(0);
  System.out.println("請輸入每行個數:");
  int numberPerLine = input.nextInt();

  //呼叫printChars
  printChars(startchar , endchar , numberPerLine);
  
 } 
 public static void printChars(char ch1 , char ch2 , int numberPerLine)
 {
  int a = ch1 ;
  int b = ch2 ;
  int count = 1;
  
  for(int i = a ; i <= b ; i ++)
  {
   
    if(count % numberPerLine == 0 )
    {
     System.out.println((char)i + " ");
     count = count + 1;
    }
    else
    {
     System.out.print((char)i + " ");
     count = count + 1;
    }    
  }
 }
 

}

沒有留言:

張貼留言

Go(Golang)程式語言 設定GCC

下載MSYS2:  https://www.msys2.org/ 安裝 程式開始執行後輸入: pacman -Syu y y 在安裝目錄底下找到msys2.exe,雙擊執行 輸入指令: pacman -Su pacman -S --needed base-devel mingw-...