1.
a=3, b=2, c=5
a+=--b+c
c-=b+a++
輸出a b c
這種題經常碰到,主要考察的是++ 和 --,只要牢記
--b是指在賦值之前使得b減一,此時--b=b-1,b=b-1;
b++是指在賦值之後使得b減一,此時a++=a,a=a+1;
所以這裏當b=2時;--b=1;b=1;當a=3時;a++=3;a=4;
2.寫一個程序:實現字符串翻轉。例如:原字符串“i am a student”轉換後“student a am i”。要求:不能使用庫函數。
這裏實現的不是很好,用字符串數組實現,因爲java裏面沒動態數組所以得固定數組大小。如果有哪個朋友能有更好的辦法記得告訴我哦!在此謝謝了!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.util.*;
/**
*
* @author Administrator
*/
public class teststream {
public static void main(String[] args){
String[] str=new String[20];
BufferedInputStream in=new BufferedInputStream(System.in);
int i;int j=0;
try {
while((i=in.read())!=10){
if((char)i==' '){
if(j==0 && str[j]==null){j=-1;}
if(str[j+1]==null) str[j+1]="";
str[j+1]+=String.valueOf((char)i);
j=j+2;
continue;
}
if(str[j]==null) str[j]="";
str[j]+=String.valueOf((char)i);
}
} catch (Exception e) {
e.printStackTrace();
}
// for(int k=(str.length-1);k>=0;k--){
for(int k=0;k<str.length;k++){
if(str[k]!=null)
System.out.print(str[k]);
}System.out.println();
for(int k=(str.length-1);k>-1;k--){
//for(int k=0;k<str.length;k++){
if(str[k]!=null)
System.out.print(str[k]);
}
System.out.print("---------");
System.out.print(j);
System.out.print(j++);
System.out.print(j++);
}
}