簡單的Java,Python,C,C++

來源:互聯網
上載者:User

標籤:har   eof   big   close   public   space   寫入   util   next   

  1 Java 語言  2 //package main  3 //注意不要添加包名稱,否則會報錯。  4   5 import java.io.*;  6 import java.util.*;  7 cin.hasNext();  8 cin.hasNextLine();  9 cin.hasNextBigDecimal(); 10 cin.hasNextBigInteger(); 11 cin.hasNextBoolean(); 12 cin.hasNextByte(); 13 cin.hasNextDouble(); 14 cin.hasNextInt(); 15 cin.hasNextLong(); 16 public class Test { 17     public static void main(String args[]) 18     { 19         Scanner cin = new Scanner(System.in); 20         int a, b; 21         while(cin.hasNextInt())//判斷cin中是否還有值 22         { 23             a = cin.nextInt(); 24             b = cin.nextInt(); 25             System.out.println(a + b); 26         } 27     } 28 } 29 public static void main(String args[]) 30     { 31        int x=1; 32     int y=~1; 33     System.out.println(Integer.toBinaryString(x));//1 34     System.out.println(Integer.toBinaryString(y));//1111111111111111111111111110 35     System.out.println(y);//-2 36     } 37  * 測試3:從文字檔中讀取資料 38      */ 39     static void testExample03(){ 40         //1、在記憶體中開啟要讀取檔案的字元流對象 41         try { 42             Reader reader=new FileReader("e:/ReadMe.log"); 43              44             //迴圈讀取,一次就讀一個 45             int ch=reader.read(); 46             StringBuffer buffer=new StringBuffer(); 47             while(ch!=-1){ //讀取成功 48                 buffer.append((char)ch); 49                 ch=reader.read(); 50             } 51             System.out.println(buffer.toString()); 52             //3、關閉流 53             reader.close(); 54         } catch (FileNotFoundException e) { 55             System.out.println("要讀取的檔案不存在:"+e.getMessage()); 56         } catch (IOException e) { 57             System.out.println("檔案讀取錯誤:"+e.getMessage()); 58         } 59     } 60    /** 61      * 測試4:向文字檔中寫入資料 62      */ 63  static void testExample04(){ 64         System.out.println("請輸入內容:"); 65         String text=input.next(); 66         try { 67             //1、開啟流 68             Writer w=new FileWriter("e:/測試.txt",true); 69             //2、寫入內容 70             w.write(text); 71             //3、關閉流 72             w.close(); 73         } catch (IOException e) { 74             System.out.println("檔案寫入錯誤:"+e.getMessage()); 75         } 76     } 77 #!/usr/bin/env python   78 # coding=utf-8   79 # Python使用的是2.7,縮排可以使用tab、4個空格或2個空格,但是只能任選其中一種,不能多種混用 80 while 1: 81     a=[]   82     s = raw_input() 83     # raw_input()裡面不要有任何提示資訊 84     if s != "": 85         for x in s.split():   86             a.append(int(x))   87             88         print sum(a) 89     else: 90         break 91  92 //C語言 93 #include   <iostream.h>    //資料流輸入/輸出  94 #include   <math.h>      //定義數學函數  95 #include   <stdio.h>        //定義輸入/輸出函數  96 #include   <stdlib.h>     //定義雜項函數及記憶體配置函數  97 #include   <string.h>     //字串處理 98 #include <stdio.h> 99 int main()100 {101    int a, b;102    while(scanf("%d%d", &a, &b) != EOF)103    printf("%d\n", a + b);104    system("pause");105 }106 107 108 //C++語言109 #include  <iostream> 110 using namespace std;111 int main()112 {113     int a, b;114     while(cin>> a >> b)115     cout << a + b << endl;116     return 0;117 }118 119 120 介紹1:Java的輸入控制121 測試2:測試輸出x=1 和y=~1122 測試3:從文字檔中讀取資料123 測試4:向文字檔中寫入資料

 

簡單的Java,Python,C,C++

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.