现在的位置: 首页 > 综合 > 正文

Java漫游—char型数据输入

2018年05月11日 ⁄ 综合 ⁄ 共 4888字 ⁄ 字号 评论关闭
Code:
  1.    今天老师让我们做一个题,也就是简单的四则运算,用Scanner sc = new Scanner(System.in) ; sc.next()的话不可以直接输入Char型数据当然nextLine()直接使用的话也相当不好用,我当初的是把符号当成String类,然后直接charAt(0);来进行判断的,很麻烦,后来看别人写的一个自己实现的nextChar()方法其实也很简单,直接定义类,然后在其中定义一个scan(InputStream input)的方法,通过next()输入然后截取,只不过可以一次完成,方便了很多,而且还有关于next()和nextLine()方法的不同。放上来晚上写的几段程序,听别人说老师生病了,希望老师快点好起来吧,快高考了,希望小杰能考上哈工大,我冬天要去找你滑冰的哦,我就在这里继续悲剧下去吧,悲剧下去把,下去把!  
  2.   
  3.   
  4.   
  5. 我的程序  
  6.   
  7. Code:  
  8. import java.util.Scanner;    
  9.     
  10. public class TestTringle{    
  11.     private double a;    
  12.     private double b;    
  13.     private double c;    
  14.     private boolean flag = false;    
  15.     public TestTringle(){}    
  16.     public void testLine(){    
  17.         if(a+b>c&&a+c>b&&b+c>a&&a-b<c&&a-c<b&&b-a<c&&b-c<a&&c-a<b&&c-b<a)    
  18.             flag=true;    
  19.         else System.out.println("error!!");    
  20.             
  21.     }    
  22.     public void isTringle(){    
  23.         if(flag) System.out.println("is a tringle");    
  24.         else System.out.println("maybe it is not  a tringle");    
  25.     }    
  26.     public void init(){    
  27.         Scanner sc = new Scanner(System.in);    
  28.         a=sc.nextInt();    
  29.         b=sc.nextInt();    
  30.         c=sc.nextInt();    
  31.         testLine();    
  32.         isTringle();    
  33.                 
  34.     
  35.     }    
  36.     
  37.     public static void main(String[] args){    
  38.         new TestTringle().init();    
  39.     }    
  40.     
  41. }    
  42. 我的程序  
  43.   
  44. Code:  
  45. import java.util.*;    
  46. import java.lang.String;    
  47. import java.lang.Math;    
  48.     
  49. public class TestYun{    
  50.     private double numOne;    
  51.     private double numTwo;      
  52.     private String s;    
  53.     private String sign;    
  54.     private int m;    
  55.     private char[] st;    
  56.     public void yunsuan(){    
  57.     /*  char temp = sign.charAt(0);  
  58.         switch(temp){  
  59.         case'+':System.out.println(numOne+numTwo); break;  
  60.         case'-': System.out.println(numOne-numTwo); break;  
  61.         case'*':System.out.println(numOne*numTwo); break;  
  62.         case'/': System.out.println(numOne/numTwo);break;  
  63.         default: System.out.println("Maybe error accurs!!");  
  64.     */int flag=0;    
  65.                 if(s.charAt(s.length()-1)=='='){    
  66.                          st= new char[s.length()-1];    
  67.                         for(int i=0;i<s.length()-2;i++){    
  68.                                     
  69.                                 if(st[i]!='+'||st[i]!='-'||st[i]!='*'||st[i]!='/'){    
  70.                                     flag++;    
  71.     
  72.                                  }    
  73.                      }    
  74.                                         for(m=flag+1;m<s.length()-2;m++){    
  75.                                                     
  76.     
  77.                                             }    
  78.                 }    
  79.         int temp = flag;    
  80.         for(int i=0;i<=flag-1;i++){    
  81.                 
  82.                 numOne+=(int)st[i]*(int)Math.pow(10,temp);    
  83.                 temp--;    
  84.     //return 2;    
  85.         }    
  86.     int temp2=st.length-flag;    
  87.     for(int i = flag+1;i<st.length;i++)    
  88.         {numTwo+=(int)st[i]*(int)Math.pow(10,temp2);    
  89.         temp2--;    
  90.         }    
  91.     switch(st[flag]){    
  92.             
  93.                     case '+': System.out.println(numOne+numTwo);break;    
  94.                     case '-': System.out.println(numOne-numTwo);break;    
  95.                     case '*': System.out.println(numOne*numTwo);break;    
  96.                     case '/': System.out.println(numOne/numTwo);break;    
  97.     
  98.     
  99.     
  100.             }    
  101.     
  102.     //Sytem.out.println();    
  103.     
  104.     }    
  105.     public void init(){    
  106.     Scanner sc = new Scanner(System.in);    
  107.     s=sc.next();    
  108.     //numOne = sc.nextDouble();    
  109.     //sign = sc.next();    
  110.     //numTwo = sc.nextDouble();    
  111.     //System.out.println(yunsuan());    
  112.     yunsuan();    
  113.     
  114.     }    
  115.     public static void main(String[] args){    
  116.         System.out.println("输入算式以等号结束!!");    
  117.         new TestYun().init();    
  118.     }    
  119.     
  120.     
  121. }    
  122. 找的别人的方法  
  123.   
  124.   
  125.   
  126. Code:  
  127. 由于Scanner是一个final类,不可以去继承.但是你可以使用面向对象的一个机制封装去实现一个nextChar操作。    
  128. import java.io.InputStream;    
  129. import java.util.InputMismatchException;    
  130. import java.util.Scanner;    
  131.     
  132. public class Scan {    
  133.  private Scanner reader;    
  134.     
  135.  public Scan(InputStream in) {    
  136.   reader = new Scanner(in);    
  137.  }    
  138.     
  139.  public boolean hasNextChar() {    
  140.   return reader.hasNext(".");    
  141.  }    
  142.     
  143.  public char nextChar() {    
  144.   String ret = reader.next();    
  145.   if (ret.length() > 1) {    
  146.    throw new InputMismatchException();    
  147.   }    
  148.   return (char)ret.charAt(0);    
  149.  }    
  150.     
  151.  public boolean hasNextInt() {    
  152.   return reader.hasNextInt();    
  153.  }    
  154.     
  155.  public boolean hasNextInt(int radix) {    
  156.   return reader.hasNextInt(radix);    
  157.  }    
  158.     
  159.  public int nextInt() {    
  160.   return reader.nextInt();    
  161.  }    
  162.     
  163.  public int nextInt(int radix) {    
  164.   return reader.nextInt(radix);    
  165.  }    
  166.     
  167.  public static void main(String[] args) {    
  168.   Scan scan = new Scan(System.in);    
  169.   System.out.println(scan.hasNextChar());    
  170.   System.out.println(scan.nextChar());    
  171.  }    
  172.     
  173. }     

 

抱歉!评论已关闭.