- 今天老师让我们做一个题,也就是简单的四则运算,用Scanner sc = new Scanner(System.in) ; sc.next()的话不可以直接输入Char型数据当然nextLine()直接使用的话也相当不好用,我当初的是把符号当成String类,然后直接charAt(0);来进行判断的,很麻烦,后来看别人写的一个自己实现的nextChar()方法其实也很简单,直接定义类,然后在其中定义一个scan(InputStream input)的方法,通过next()输入然后截取,只不过可以一次完成,方便了很多,而且还有关于next()和nextLine()方法的不同。放上来晚上写的几段程序,听别人说老师生病了,希望老师快点好起来吧,快高考了,希望小杰能考上哈工大,我冬天要去找你滑冰的哦,我就在这里继续悲剧下去吧,悲剧下去把,下去把!
- 我的程序
- Code:
- import java.util.Scanner;
- public class TestTringle{
- private double a;
- private double b;
- private double c;
- private boolean flag = false;
- public TestTringle(){}
- public void testLine(){
- 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)
- flag=true;
- else System.out.println("error!!");
- }
- public void isTringle(){
- if(flag) System.out.println("is a tringle");
- else System.out.println("maybe it is not a tringle");
- }
- public void init(){
- Scanner sc = new Scanner(System.in);
- a=sc.nextInt();
- b=sc.nextInt();
- c=sc.nextInt();
- testLine();
- isTringle();
- }
- public static void main(String[] args){
- new TestTringle().init();
- }
- }
- 我的程序
- Code:
- import java.util.*;
- import java.lang.String;
- import java.lang.Math;
- public class TestYun{
- private double numOne;
- private double numTwo;
- private String s;
- private String sign;
- private int m;
- private char[] st;
- public void yunsuan(){
- /* char temp = sign.charAt(0);
- switch(temp){
- case'+':System.out.println(numOne+numTwo); break;
- case'-': System.out.println(numOne-numTwo); break;
- case'*':System.out.println(numOne*numTwo); break;
- case'/': System.out.println(numOne/numTwo);break;
- default: System.out.println("Maybe error accurs!!");
- */int flag=0;
- if(s.charAt(s.length()-1)=='='){
- st= new char[s.length()-1];
- for(int i=0;i<s.length()-2;i++){
- if(st[i]!='+'||st[i]!='-'||st[i]!='*'||st[i]!='/'){
- flag++;
- }
- }
- for(m=flag+1;m<s.length()-2;m++){
- }
- }
- int temp = flag;
- for(int i=0;i<=flag-1;i++){
- numOne+=(int)st[i]*(int)Math.pow(10,temp);
- temp--;
- //return 2;
- }
- int temp2=st.length-flag;
- for(int i = flag+1;i<st.length;i++)
- {numTwo+=(int)st[i]*(int)Math.pow(10,temp2);
- temp2--;
- }
- switch(st[flag]){
- case '+': System.out.println(numOne+numTwo);break;
- case '-': System.out.println(numOne-numTwo);break;
- case '*': System.out.println(numOne*numTwo);break;
- case '/': System.out.println(numOne/numTwo);break;
- }
- //Sytem.out.println();
- }
- public void init(){
- Scanner sc = new Scanner(System.in);
- s=sc.next();
- //numOne = sc.nextDouble();
- //sign = sc.next();
- //numTwo = sc.nextDouble();
- //System.out.println(yunsuan());
- yunsuan();
- }
- public static void main(String[] args){
- System.out.println("输入算式以等号结束!!");
- new TestYun().init();
- }
- }
- 找的别人的方法
- Code:
- 由于Scanner是一个final类,不可以去继承.但是你可以使用面向对象的一个机制封装去实现一个nextChar操作。
- import java.io.InputStream;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Scan {
- private Scanner reader;
- public Scan(InputStream in) {
- reader = new Scanner(in);
- }
- public boolean hasNextChar() {
- return reader.hasNext(".");
- }
- public char nextChar() {
- String ret = reader.next();
- if (ret.length() > 1) {
- throw new InputMismatchException();
- }
- return (char)ret.charAt(0);
- }
- public boolean hasNextInt() {
- return reader.hasNextInt();
- }
- public boolean hasNextInt(int radix) {
- return reader.hasNextInt(radix);
- }
- public int nextInt() {
- return reader.nextInt();
- }
- public int nextInt(int radix) {
- return reader.nextInt(radix);
- }
- public static void main(String[] args) {
- Scan scan = new Scan(System.in);
- System.out.println(scan.hasNextChar());
- System.out.println(scan.nextChar());
- }
- }