Author: Not specified Language: html4strict
Description: Not specified Timestamp: 2017-08-10 08:28:58 +0000
View raw paste Reply
  1. <pre style="background:#000;color:#f8f8f8"><span style="color:#e28964">package</span> <span style="color:#99cf50">com.cbw.project0810</span>;
  2.  
  3. <span style="color:#99cf50">public</span> <span style="color:#99cf50">class</span> <span style="text-decoration:underline">Test02</span> {
  4.  
  5.     <span style="color:#99cf50">public</span> <span style="color:#99cf50">static</span> <span style="color:#99cf50">void</span> <span style="color:#89bdff">main</span>(<span style="color:#99cf50">String</span>[] <span style="color:#3e87e3">args</span>) {
  6.        
  7.         <span style="color:#99cf50">Rand4</span> r1 <span style="color:#e28964">=</span> <span style="color:#e28964">new</span> <span style="color:#99cf50">Rand4</span>();
  8.         r1<span style="color:#e28964">.</span>putArr();
  9.         r1<span style="color:#e28964">.</span>putZero();
  10.        
  11.     }
  12. }
  13. <span style="color:#99cf50">class</span> <span style="text-decoration:underline">Rand4</span> {
  14.     <span style="color:#99cf50">int</span>[][] arr <span style="color:#e28964">=</span> <span style="color:#e28964">new</span> <span style="color:#99cf50">int</span>[<span style="color:#3387cc">4</span>][<span style="color:#3387cc">4</span>];
  15.     <span style="color:#99cf50">int</span> cnt<span style="color:#e28964">=</span><span style="color:#3387cc">0</span>;
  16.     <span style="color:#99cf50">void</span> <span style="color:#89bdff">putArr</span>() {
  17.         <span style="color:#e28964">for</span>(<span style="color:#99cf50">int</span> i<span style="color:#e28964">=</span><span style="color:#3387cc">0</span>; i<span style="color:#e28964">&lt;</span>arr<span style="color:#e28964">.</span>length;i<span style="color:#e28964">++</span>) {
  18.             <span style="color:#e28964">for</span>(<span style="color:#99cf50">int</span> j<span style="color:#e28964">=</span><span style="color:#3387cc">0</span>; j<span style="color:#e28964">&lt;</span>arr[<span style="color:#3387cc">0</span>]<span style="color:#e28964">.</span>length; j<span style="color:#e28964">++</span>) {
  19.                 arr[i][j] <span style="color:#e28964">=</span> (<span style="color:#99cf50">int</span>)(<span style="color:#99cf50">Math</span><span style="color:#e28964">.</span>random()<span style="color:#e28964">*</span><span style="color:#3387cc">8</span>)<span style="color:#e28964">+</span><span style="color:#3387cc">1</span>;
  20.             }
  21.         }
  22.     }
  23.     <span style="color:#99cf50">void</span> <span style="color:#89bdff">putZero</span>() {
  24.         <span style="color:#e28964">for</span>(<span style="color:#99cf50">int</span> i<span style="color:#e28964">=</span><span style="color:#3387cc">0</span>; i<span style="color:#e28964">&lt;</span>arr<span style="color:#e28964">.</span>length;i<span style="color:#e28964">++</span>) {
  25.             <span style="color:#e28964">for</span>(<span style="color:#99cf50">int</span> j<span style="color:#e28964">=</span><span style="color:#3387cc">0</span>; j<span style="color:#e28964">&lt;</span>arr[<span style="color:#3387cc">0</span>]<span style="color:#e28964">.</span>length; j<span style="color:#e28964">++</span>) {
  26.                 <span style="color:#e28964">if</span>((<span style="color:#99cf50">Math</span><span style="color:#e28964">.</span>random()<span style="color:#e28964">*</span><span style="color:#3387cc">2</span>) <span style="color:#e28964">>=</span> <span style="color:#3387cc">1</span>) {
  27.                     arr[i][j] <span style="color:#e28964">=</span> <span style="color:#3387cc">0</span>;
  28.                 }
  29.                 findZero();
  30.                 <span style="color:#e28964">if</span>(cnt<span style="color:#e28964">==</span><span style="color:#3387cc">8</span>) {
  31.                     printArr();
  32.                     <span style="color:#e28964">return</span>;
  33.                 }
  34.             }
  35.         }
  36.     }
  37.    
  38.     <span style="color:#99cf50">void</span> <span style="color:#89bdff">findZero</span>() {
  39.         cnt<span style="color:#e28964">=</span><span style="color:#3387cc">0</span>;
  40.         <span style="color:#e28964">for</span>(<span style="color:#99cf50">int</span> i<span style="color:#e28964">=</span><span style="color:#3387cc">0</span>; i<span style="color:#e28964">&lt;</span>arr<span style="color:#e28964">.</span>length;i<span style="color:#e28964">++</span>) {
  41.             <span style="color:#e28964">for</span>(<span style="color:#99cf50">int</span> j<span style="color:#e28964">=</span><span style="color:#3387cc">0</span>; j<span style="color:#e28964">&lt;</span>arr[<span style="color:#3387cc">0</span>]<span style="color:#e28964">.</span>length; j<span style="color:#e28964">++</span>) {
  42.                 <span style="color:#e28964">if</span>(arr[i][j] <span style="color:#e28964">==</span> <span style="color:#3387cc">0</span>) {
  43.                     cnt <span style="color:#e28964">+=</span> <span style="color:#3387cc">1</span>;
  44.                 }
  45.             }
  46.         }
  47.     }
  48.    
  49.     <span style="color:#99cf50">void</span> <span style="color:#89bdff">printArr</span>() {
  50.         <span style="color:#e28964">for</span>(<span style="color:#99cf50">int</span> i<span style="color:#e28964">=</span><span style="color:#3387cc">0</span>; i<span style="color:#e28964">&lt;</span>arr<span style="color:#e28964">.</span>length;i<span style="color:#e28964">++</span>) {
  51.             <span style="color:#e28964">for</span>(<span style="color:#99cf50">int</span> j<span style="color:#e28964">=</span><span style="color:#3387cc">0</span>; j<span style="color:#e28964">&lt;</span>arr[<span style="color:#3387cc">0</span>]<span style="color:#e28964">.</span>length; j<span style="color:#e28964">++</span>) {
  52.                 <span style="color:#99cf50">System</span><span style="color:#e28964">.</span>out<span style="color:#e28964">.</span>print(arr[i][j] <span style="color:#e28964">+</span> <span style="color:#65b042">" "</span>);
  53.             }
  54.             <span style="color:#99cf50">System</span><span style="color:#e28964">.</span>out<span style="color:#e28964">.</span>println();
  55.         }
  56.     }
  57. }
  58. </pre>
View raw paste Reply